日期:2014-05-16  浏览次数:20369 次

Hibernate和JDBC实现CRUD操作的比较
使用JDBC实现CRUD操作:
查询操作:
public class UserDAO {
  public User findUserById(int id){
    User userInfo = null;	Connection  con = DBConnection.getConnection();
    PreparedStatement pst = null;   String sql = “select * from userInfo where id=? ”;	
    ResultSet rs = null;
    try {
      pst = con.prepareStatement(sql);	pst.setInt(1,id);
      rs = pst.executeQuery();
      if(rs.next()){
        userInfo = new User();	userInfo.setId(rs.getInt("id"));
        userInfo.setPassword(rs.getString("pass"));
        userInfo.setUserName(rs.getString(“user_name"));
      }
    } catch (SQLException e) {e.printStackTrace(); }
    finally{
      try {      rs.close();      pst.close();      con.close();      } catch (Exception e) { }
    } 
    return userInfo;
  }


添加操作:

public void addUser(User user) throws Exception{
Connection con = DBConnection.getConnection();
PreparedStatement pst = null; 
String sql = “insert into  userInfo(user_name,pass) values(?,?)”;
try {
pst = con.prepareStatement(sql);
pst.setString(1, user.getUserName());
pst.setString(2,user.getPassword());
pst.executeUpdate();
}finally{
	。。。
}
}


删除操作:

public void deleteUser(int id)  throws Exception{
Connection con = DBConnection.getConnection();
PreparedStatement pst = null; 
String sql = “delete from  userInfo where id = ?”;
try {
pst = con.prepareStatement(sql);
pst.setInt(1, id);
pst.executeUpdate();
}finally{
	。。。
}
}


更新操作:

public void updateUser(User user) throws Exception {
Connection con = DBConnection.getConnection();
PreparedStatement pst = null; 
String sql = “update  userInfo set name = ? ,password = ? where id = ?”;
try {
pst = con.prepareStatement(sql);
pst.setString(1,user.getName());
pst.setString(2,user.getPassword());
pst.setInt(3, id);
pst.executeUpdate();
} catch (SQLException e) {
	e.printStackTrace();
}finally{
	。。。
}
}

?

使用Hibernate实现CRUD操作:

public class UserDAO {
public User findUserById(int id){
	return session.get(User.class,id);
}
public void addUser(User user){
	session.save(user);
}
public void updateUser(User user){
		session.update(user);
}
public void deleteUser(int id){
		User user = new User();
		user.setId(id);
		session.delete(user);
}
}

?

类型一:

??????? //得到Configuration
??? ??? Configuration configuration=new Configuration().configure();
??? ??? //得到一个会话工厂
??? ??? SessionFactory sessionFactory=configuration.buildSessionFactory();
??? ??? //得到一个Session对象
??? ??? Session session=sessionFactory.openSession();
??? ??? //开始一个事务
??? ??? Transaction transaction=session.beginTransaction();
??? ??? /*//添加一个book
??? ??? Books book=new Books();
??? ??? book.setAuthor("c");
??? ??? book.setName("wh");
??? ??? session.save (book);*/
??? ??? /*//查询book
??? ??? Books book=(Books) session.load(Books.class, 2);
??? ??? System.out.println(book.getAuthor());*/
??? ??? /*//更新book
??? ??? Books book=(Books) session.load(Books.class, 2);
??? ??? book.setAuthor("chen");
??? ??? book.setName("Spring");*/
??? ??? /*//删除book
??? ??? Books book=(Books) session.load(Books.class, 14);
??? ??? session.delete (book);*/