可以打印出圖片的大小來,測(cè)試的時(shí)候是綠色的條, 但是數(shù)據(jù)庫里面沒有任何數(shù)據(jù)。
代碼如下:
@Test
public void writePicture() throws Exception{
Student student5 = new Student(005, "Naruto", "男", new Date(), "火之國(guó)");
File pictureFile = new File("c:"+File.separator+"zhang"+File.separator+"Naruto.jpg");
//System.out.println(pictureFile);
InputStream input;
try {
input = new FileInputStream(pictureFile);
Blob image = Hibernate.getLobCreator(session).createBlob(input, input.available());
System.out.println("照片長(zhǎng)度:"+image.length()); ? //這時(shí)候打印出的數(shù)據(jù)是:10505
student5.setPicture(image);
session.save(student5);
System.out.println("保存成功!");
} catch (FileNotFoundException e) {
System.out.println("圖片文件沒找到");
e.printStackTrace();
}catch (IOException e) {
System.out.println("Bolb對(duì)象創(chuàng)建失敗,input.available()錯(cuò)誤!");
e.printStackTrace();
}
}
這時(shí)候測(cè)試時(shí),是綠色的條,但是數(shù)據(jù)庫里面沒有數(shù)據(jù),其他id什么的數(shù)據(jù)都沒存進(jìn)去?這是為什么呢?
2017-02-22
謝謝大家 ?解決了。
得加上這個(gè):
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
connection.setAutoCommit(true);
}
});
都糊涂了?。?!好低級(jí)的錯(cuò)誤,吃飯回來后才看到。