Hibernate和JDBC实现CRUD操作的比较
来源:互联网 发布:服装erp软件排行 编辑:程序博客网 时间:2024/05/21 11:06
查询操作:
- 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 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 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 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{
- 。。。
- }
- }
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);
- }
- }
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);*/
//提交事务
transaction.commit();
//关闭session
session.close();
类型二:
Configuration configuration=new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
/*//更新
Query query=session.createQuery("update Books set name='hibernate' where id=:id");
query.setInteger("id", 1);
query.executeUpdate();*/
//查询所有字段
/*Query query=session.createQuery("from Books");
List<Books > list=query.list();
for(Books book:list){
System.out.println("作者:"+book.getAuthor());
}*/
/*//查询多个字段
Query query=session.createQuery("select name,author from Books");
List<Object[] > list2=query.list();
for(Object[] object:list2){
String author=(String) object[1];
System.out.println("作者:"+author);
}*/
//查询一个字段
/*Query query=session.createQuery("select author from Books");
List<String > list3=query.list();
for(String author:list3){
System.out.println("作者:"+author);
}*/
transaction.commit();
if(session!=null||!"".equals(session)){
System.out.println("关闭session。。。。");
}
//类型三:
criteria接口 的简单使用
ts=session.beginTransaction();
Criteria cri=session.createCriteria(Employee.class).
setMaxResults(2).addOrder(Order.desc("id") );
List<Employee> list=cri.list();
for(Employee e: list){
System.out.println(e.getAaaid());
}
ts.commit();
- Hibernate和JDBC实现CRUD操作的比较
- hibernate的crud操作
- hibernate的CRUD操作
- hibernate的CRUD操作
- Hibernate的CRUD操作
- hibernate的crud操作
- jdbc简介和 crud操作
- jdbc简介和 crud操作
- JDBC简单的CRUD操作
- 封装jdbc的crud操作
- JDBC(一 JDBC的基本操作CRUD)
- Hibernate对象的CRUD操作
- maven hibernate 实现对mysql简单的CRUD操作
- Hibernate实现数据的增删改查(CRUD)操作
- JDBC和hibernate,mybatis的比较
- hibernate 入门操作----实现crud 操作
- hibernate 配置--入门操作----实现crud 操作
- Hibernate实现简单的CRUD
- interpret of gdb "gdb info reg"
- 埋藏
- Hibernate常用代码模板
- Hibernate学习小结之实体对象之间关系
- Java新浪微博客户端开发
- Hibernate和JDBC实现CRUD操作的比较
- ORA-28000: the account is locked-的解决办法
- AT&T寻址
- 给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序。
- ConnectStream下载网络图片转成MemorySteam下载到本地
- nyoj fibonacci数列(二) 矩阵乘法
- 我怀念的是
- 会员分享几个平时看榜单常去的网站
- 把字符串数组转换为文本