static Session session = HibernateSessionFactory.getSession();
public boolean inserted(TUser obj) {
Transaction tran = session.beginTransaction();
session.save(obj);
session.flush();//调用flush强制先执行select for update
session.refresh(obj, LockMode.UPGRADE);
try {
SerializableBlob sb = (SerializableBlob) obj.getImage();
java.sql.Blob wrapblob = sb.getWrappedBlob();
if (wrapblob instanceof oracle.sql.BLOB) {
//向Blob写入实际内容
oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapblob;
OutputStream output = blob.getBinaryOutputStream();
//修改你要存入的信息
FileInputStream input = new FileInputStream("E:/download/SPECIAL ENGLISH TV 2006 1203.wmv");
byte[] buf = new byte[10240];// 做为10K的缓存写入
int len;
while ((len = input.read(buf)) > 0) {
output.write(buf, 0, len);
}
output.close();
input.close();
}
SerializableClob sc = (SerializableClob) obj.getResume();
java.sql.Clob wrapclob = sc.getWrappedClob();
if (wrapclob instanceof oracle.sql.CLOB) {
oracle.sql.CLOB clob = (oracle.sql.CLOB) wrapclob;
java.io.Writer cout = clob.getCharacterOutputStream();
File file = new File("e:/errors.txt");// 修改你要存入的文本
FileInputStream fileintput = new FileInputStream(file);
int read;
while ((read = fileintput.read()) != -1) {
cout.write(read);
}
fileintput.close();
cout.close();
}
tran.commit();
return true;
} catch (Exception ex) {
ex.printStackTrace();
tran.rollback();
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
TUser stu = new TUser();
stu.setAge(new Integer("23"));
stu.setName("zengbo");
stu.setImage(Hibernate.createBlob(new byte[1]));
stu.setResume(Hibernate.createClob(" "));//插入一个空的参数
t.inserted(stu);
}
}
事例2
public class Test2 {
public static void main(String[] args) throws SQLException, IOException {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
TUser user = new TUser();
user.setAge((long) 24);
user.setName("zengbo");
user.setImage(Hibernate.createBlob(new byte[1]));
user.setResume(Hibernate.createClob(""));
session.save(user);
session.flush();
session.refresh(user, LockMode.UPGRADE);
SerializableBlob sb = (SerializableBlob) user.getImage();
java.sql.Blob wrapblob = sb.getWrappedBlob();
oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapblob;
OutputStream output = blob.getBinaryOutputStream();
FileInputStream input = new FileInputStream("E:/download/UML.rar");
byte[] b = new byte[10240];// 做为10K的缓存写入
input.read(b);
output.write(b);
input.close();
output.close();
//resume由Clob改成String类型存入Clob就可以这样写
// SerializableClob sc = (SerializableClob) user.getResume();
// java.sql.Clob wrapclob = sc.getWrappedClob();
// oracle.sql.CLOB clob = (oracle.sql.CLOB) wrapclob;
// java.io.Writer writer = clob.getCharacterOutputStream();
// writer.write("This is the second clob");
// session.save(user);
// session.flush();
tx.commit();
}
}