java 关于blob类型问题

java中如何处理blob类型的问题
2024-12-23 09:12:55
推荐回答(2个)
回答1:

public void save(String vid,String title,String type,String user,String date,String context,String file) throws Exception
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "insert into news(v_id,title,type,person,inputtime,context,attach_docid) values('"+vid+"','"+title+"','"+type+"','"+user+"','"+date+"',EMPTY_BLOB(),'"+file+"')";
try {
DBJdbc dbjdbc = new DBJdbc();
conn = dbjdbc.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
String sql1 = "select context from news where v_id ='"+vid+"' for update"; // 使用"FOR UPDATE"得到表的写锁
rs = stmt.executeQuery(sql1);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob(1); // 得到BLOB对象
OutputStream outout = blob.getBinaryOutputStream(); // 建立输出流
InputStream in = new ByteArrayInputStream(strss.HTMLEncode(context).getBytes()); //字符串转换为数据流
int size = blob.getBufferSize();
byte[] buffer = new byte[size]; // 建立缓冲区
int len;
while ((len = in.read(buffer)) != -1)
outout.write(buffer, 0, len);
in.close();
outout.close();
}
conn.commit();
stmt.close();
conn.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
希望能帮助到你

回答2:

用byte类型