File file = new File(filePath);
Connection conn = getConnection();
try {
java.sql.Statement st = conn.createStatement();
conn.setAutoCommit(false);
st.execute("insert into table_name values(1,empty_blob())");
ResultSet rs = st
.executeQuery("select id,word from table_name where id=1 for update");
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("word");
OutputStream outStream = blob.getBinaryOutputStream();
InputStream fin = new FileInputStream(file);
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
fin.close();
outStream.flush();
outStream.close();
conn.commit();
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Connection conn = getConnection();
java.sql.Statement st;
try {
st = conn.createStatement();
ResultSet rs = st.executeQuery("select id,word from table_name where id='"+id+"'");
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("word");
File file = new File("c://filename.doc");
FileOutputStream output = new FileOutputStream(file);
InputStream input = blob.getBinaryStream();
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn=getConnection();
Statement stem=null;
ResultSet rs=null;
try{
conn.setAutoCommit(false);
stem=conn.createStatement();
rs = stem.executeQuery("select word from table_name where id='"+id+"' for update");
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("word");
OutputStream outStream = blob.getBinaryOutputStream();
InputStream fin = new FileInputStream("c://2.doc");
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
fin.close();
outStream.flush();
outStream.close();
conn.commit();
conn.close();
}
} catch (Exception ex){
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ex.getMessage());
}
}