Hibernate 基本写法
Configuration cfg = new Configuration().configure();//configure("hibernate.cfg.xml")
SessionFactory sf = cfg.buildSessionFactory();
Session ss = sf.openSession();
Transaction ts = ss.beginTransaction();
Query query = ss.createQuery("from Teacher");
List list = query.list();
.....
ts.commit();
ss.close();
在jdbc中使用事务 要手动提交时:
Connection conn = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
conn = DriverManager.getConnection(url,"sa","123");
conn.setAutoCommit(false);//设置手动提交
PreparedStatement pst = conn.prepareStatement("select top 10 * from a_tmpobj");
ResultSet rs = pst.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1));
}
System.out.println("yes");
conn.commit;//未报错提交事务
} catch (ClassNotFoundException e){
conn.rollback;//报错事务回滚
// TODO Auto-generated catch block
e.printStackTrace();
}
另:
Hibernate中query.setInteger()从0开始
Jdbc 中 PreparedStatement.setInt() 从1开始
附一张带sql2000jdbc的必须jar包