89ou 发表于 2016-11-16 00:21:27

将文件整个保存到DB2数据库BLOB字段

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class ExcelImportServiceImpl implements ExcelImportService {
public void excelImport(String filePath) {
Connection conn = null;
PreparedStatementps = null;
try{
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection("jdbc:db2://localhost:50000/xxx","username","password");
ps = conn.prepareStatement("insert into aa.user(detail) values (?)");
File file = new File(filePath);
InputStream is = new FileInputStream(file);
ps.setBinaryStream(1, is, (int)file.length());
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
if(ps!=null){
try{
ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}

  如出现以下错误:
  com.ibm.db2.jcc.am.co: DB2 SQL Error: SQLCODE=-302, SQLSTATE=22001, SQLERRMC=null, DRIVER=3.57.82
  原因是:上传文件所需内存大于数据库字段的最大内存限制
   
页: [1]
查看完整版本: 将文件整个保存到DB2数据库BLOB字段