当前位置: 技术问答>java相关
BLOB问题,郁闷好几天了,求救大家了!!!
来源: 互联网 发布时间:2017-04-11
本文导语: public void insertBlob(){ String sql = "insert into uploadfile(filecontent,file_id) values(?,?)"; Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); ps = conn.prepareStatement(sql); ...
public void insertBlob(){
String sql = "insert into uploadfile(filecontent,file_id) values(?,?)";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
//插入BLOB
conn.setAutoCommit(false);
File file = new File("d:\test.txt");
InputStream is = new FileInputStream(file);
byte[] blobByte=new byte[is.available()];
is.read(blobByte);
is.close();
ps.setBinaryStream(1,(new ByteArrayInputStream(blobByte)),blobByte.length);
ps.setString(2,getNextId());
ps.execute();
conn.setAutoCommit(true);
}catch(Exception ex) {
ex.printStackTrace();
try{
if(conn.isClosed()){
System.out.println(" connection is closed!");
}
}catch(Exception ex2){
ex2.printStackTrace();
}
}
finally {
try {
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}catch(Exception e) {e.printStackTrace();}
System.out.println(sql);
}
}
抛出这样的错误:
java.sql.SQLException: Io exception: Connection reset by peer: socket write error
这怎么解决???
String sql = "insert into uploadfile(filecontent,file_id) values(?,?)";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
//插入BLOB
conn.setAutoCommit(false);
File file = new File("d:\test.txt");
InputStream is = new FileInputStream(file);
byte[] blobByte=new byte[is.available()];
is.read(blobByte);
is.close();
ps.setBinaryStream(1,(new ByteArrayInputStream(blobByte)),blobByte.length);
ps.setString(2,getNextId());
ps.execute();
conn.setAutoCommit(true);
}catch(Exception ex) {
ex.printStackTrace();
try{
if(conn.isClosed()){
System.out.println(" connection is closed!");
}
}catch(Exception ex2){
ex2.printStackTrace();
}
}
finally {
try {
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}catch(Exception e) {e.printStackTrace();}
System.out.println(sql);
}
}
抛出这样的错误:
java.sql.SQLException: Io exception: Connection reset by peer: socket write error
这怎么解决???
|
pstmt.setBinaryStream(1, fileinput, (int)f1.length() );
can insert only upto 4k of data, if you need to do more than 4k then you got to get a Blob out and then stream the data into the blob.
http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/LOBSample.java.html
can insert only upto 4k of data, if you need to do more than 4k then you got to get a Blob out and then stream the data into the blob.
http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/LOBSample.java.html