我以前的一段代码,自己分析吧
public static synchronized int add(PostAttachment pa) {
Connection con = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
con = MyConnection.getForumConnection();
pstat = con.prepareStatement(
"select max(POST_ATTACHMENT_ID) from POST_ATTACHMENT");
rs = pstat.executeQuery();
int nextid = 1;
if (rs.next()) {
nextid = rs.getInt(1) + 1;
}
pstat.close(); con.setAutoCommit(false); pstat = con.prepareStatement(
"insert into POST_ATTACHMENT (POST_ATTACHMENT_ID,FILE_NAME,CONTENT_TYPE,FILE_SIZE,POST_ID,SEQUENCE_NO,CONTENT) values(?,?,?,?,?,?,EMPTY_BLOB())");
pstat.setInt(1, nextid);
pstat.setString(2, pa.getFileName());
pstat.setString(3, pa.getContentType());
pstat.setInt(4, pa.getFileSize());
pstat.setInt(5, pa.getPostId());
pstat.setInt(6, pa.getSequenceNo());
if (pstat.executeUpdate() == 1) {
if (pa.getContent() != null) {
pstat.close();
pstat = con.prepareStatement(
"select CONTENT from POST_ATTACHMENT where POST_ATTACHMENT_ID=? for UPDATE");
pstat.setInt(1, nextid);
rs = pstat.executeQuery();
rs.next();
java.sql.Blob blob = null;
blob = rs.getBlob("CONTENT");
java.io.OutputStream os = ((weblogic.jdbc.vendor.oracle.
OracleThinBlob) blob).
getBinaryOutputStream();
os.write(pa.getContent());
os.flush();
os.close();
}
con.commit();
return nextid;
} else {
return -1;
}
} catch (Exception ex) {
try {
con.rollback();
} catch (Exception ex2) {}
Logger.error("net.java2000.user.PostDAO", "add()", ex);
return -1;
} finally {
if (con != null) {
try {
con.setAutoCommit(true);
} catch (Exception ex) {}
}
MyConnection.closeConnection(con, pstat, rs);
}
}
public static synchronized int add(PostAttachment pa) {
Connection con = null;
PreparedStatement pstat = null;
ResultSet rs = null;
try {
con = MyConnection.getForumConnection();
pstat = con.prepareStatement(
"select max(POST_ATTACHMENT_ID) from POST_ATTACHMENT");
rs = pstat.executeQuery();
int nextid = 1;
if (rs.next()) {
nextid = rs.getInt(1) + 1;
}
pstat.close(); con.setAutoCommit(false); pstat = con.prepareStatement(
"insert into POST_ATTACHMENT (POST_ATTACHMENT_ID,FILE_NAME,CONTENT_TYPE,FILE_SIZE,POST_ID,SEQUENCE_NO,CONTENT) values(?,?,?,?,?,?,EMPTY_BLOB())");
pstat.setInt(1, nextid);
pstat.setString(2, pa.getFileName());
pstat.setString(3, pa.getContentType());
pstat.setInt(4, pa.getFileSize());
pstat.setInt(5, pa.getPostId());
pstat.setInt(6, pa.getSequenceNo());
if (pstat.executeUpdate() == 1) {
if (pa.getContent() != null) {
pstat.close();
pstat = con.prepareStatement(
"select CONTENT from POST_ATTACHMENT where POST_ATTACHMENT_ID=? for UPDATE");
pstat.setInt(1, nextid);
rs = pstat.executeQuery();
rs.next();
java.sql.Blob blob = null;
blob = rs.getBlob("CONTENT");
java.io.OutputStream os = ((weblogic.jdbc.vendor.oracle.
OracleThinBlob) blob).
getBinaryOutputStream();
os.write(pa.getContent());
os.flush();
os.close();
}
con.commit();
return nextid;
} else {
return -1;
}
} catch (Exception ex) {
try {
con.rollback();
} catch (Exception ex2) {}
Logger.error("net.java2000.user.PostDAO", "add()", ex);
return -1;
} finally {
if (con != null) {
try {
con.setAutoCommit(true);
} catch (Exception ex) {}
}
MyConnection.closeConnection(con, pstat, rs);
}
}
InputStream is = b_from.getBinaryStream();
BufferedInputStream input = new BufferedInputStream(is);
byte[] buff = new byte[2048];
while(-1 != (bytesRead = input.read(buff, 0, buff.length))) {
//在这里执行写入,如写入到文件的BufferedOutputStream里
System.out.println(bytesRead);
}
去这个Blog看看,我有例子:
http://www.3y11.com
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;import oracle.sql.BLOB;import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Hibernate;
import org.hibernate.lob.SerializableBlob;import com.test.HibernateSessionFactory;public class TestBlob { /**
* @param args
* @throws SQLException
* @throws IOException
*/
public static void main(String[] args) throws SQLException, IOException {
Session session =HibernateSessionFactory.getSession();
Transaction t=session.beginTransaction();
byte[] b=new byte[1];
Phototest p=new Phototest();
p.setImgfile(Hibernate.createBlob(b));
session.save(p);
session.flush();
session.refresh(p, LockMode.UPGRADE);
SerializableBlob sb = (SerializableBlob)p.getImgfile();
java.sql.Blob wrapBlob = sb.getWrappedBlob();
oracle.sql.BLOB blob = (oracle.sql.BLOB)wrapBlob;
OutputStream os=blob.getBinaryOutputStream();
FileInputStream fis=new FileInputStream("1.gif");
byte[] data = new byte[(int)fis.available()];
fis.read(data);
os.write(data);
t.commit();
session.close(); }}