åè代ç å¦ä¸ï¼
public class InsertBlobData {
Connection con = null;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InsertBlobData data = new InsertBlobData();
data.insertBlogInfo("002jpg", "sdsdfdf", "2007-02-12", "002.jpg");
}
public void insertBlogInfo(String jmzh, String xm, String smsj,
String fileName) throws Exception {
// try {
con = ConnectionPoliceFactory.getFactory().getConnection();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// å¤çäºå¡
boolean defaultCommit = con.getAutoCommit();
con.setAutoCommit(false);
Statement st = con.createStatement();
// æå
¥ä¸ä¸ªç©ºå¯¹è±¡
st.executeUpdate("insert into ksren_txxx(jmzh,xm,smsj,txsj) values('"
+ jmzh + "','" + xm + "',to_date('" + smsj
+ "','yyyy-mm-dd'),empty_blob())");
// ç¨for updateæ¹å¼éå®æ°æ®è¡
ResultSet rs = st
.executeQuery("select txsj from ksren_txxx where jmzh='"
+ jmzh + "' and xm='" + xm + "' for update");
if (rs.next()) {
// å¾å°java.sql.Blob对象ï¼ç¶åCast为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
// å°æ°æ®åºçè¾åºæµ
OutputStream outStream = blob.getBinaryOutputStream();
// è¿éç¨ä¸ä¸ªæ件模æè¾å
¥æµ
InputStream fin = new FileInputStream(new File(fileName));
// å°è¾å
¥æµåå°è¾åºæµ
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
// blob.putBytes(1,b);
}
// ä¾æ¬¡å
³éï¼æ³¨æ顺åºï¼
fin.close();
outStream.flush();
outStream.close();
con.commit();
/* æ¢å¤åæ交ç¶æ */
con.setAutoCommit(defaultCommit);
con.close();
}
}
}
温馨提示:内容为网友见解,仅供参考