将开发过程中经常用的一些内容片段做个珍藏,下面资料是关于java大对象存取的简单实现的内容。
创新互联是专业的柳林网站建设公司,柳林接单;提供成都网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行柳林网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
package info.itlanguageexpress.LargeObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLLargeObjectAccess {
public static String DRIVER = "org.gjt.mm.mysql.Driver";
public static String USER = "root";
public static String PASSWORD = "your password";
public static void main(String[] args) throws Exception {
Connection conn = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
insertWordFileToMySql(conn);
fetchWordFileFromMySql(conn);
} catch (Exception ex) {
ex.printStackTrace(System.out);
} finally {
try {
conn.close();
} catch (Exception ex) {
}
}
}
public static void insertWordFileToMySql(Connection conn)
throws SQLException, IOException {
PreparedStatement pstm = conn
.prepareStatement("insert into wordInputTest values (?,?)");
pstm.setString(1, "docx1");
InputStream in = new FileInputStream("F:" + File.separator
+ "test1.docx");
pstm.setBinaryStream(2, in, in.available());
pstm.executeUpdate();
pstm.close();
}
public static void fetchWordFileFromMySql(Connection conn)
throws SQLException, IOException {
PreparedStatement pstm = conn
pstm.setString(1, "docx1");
ResultSet rs = pstm.executeQuery();
int count = 1;
while(rs.next()) {
InputStream in = rs.getBinaryStream("file");
FileOutputStream out = new FileOutputStream("F:" + File.separator + "testcopy" + count + ".docx");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
count++;
}
rs.close();
pstm.close();
}
}
分享标题:java大对象存取的简单实现的代码
网页URL:https://www.cdcxhl.com/article4/poegie.html
成都网站建设公司_创新互联,为您提供商城网站、网站内链、Google、定制网站、网站设计公司、网站策划
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联