📄 studentdao.java
字号:
/*
* 创建日期 2005-7-2
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package persistence;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Clob;
import model.Student;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class StudentDAO extends BaseDAO {
static Session session = null;
static Transaction tx = null;
public static Student findStu() throws IOException {
Student stu=new Student();;
try {
session = HibernateUtil.currentSession(); //开启连接
tx = session.beginTransaction(); //开启事务
FileInputStream fileInputStream = new FileInputStream("src\\sample.jpg");
Blob photo = Hibernate.createBlob(fileInputStream);
Clob describe = Hibernate.createClob("he is so good student");
stu.setName("tomclus");
stu.setAge(new Integer(30));
stu.setPhoto(photo);
stu.setDescribe(describe);
session.save(stu);
tx.commit();
} catch (HibernateException e) { //捕捉例外
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession(session);
}
return stu;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -