hibernate ADUQ操作


  //增加
  public void addBook(Book book) {
    Session session=getSession();
    Transaction tr=session.beginTransaction();
    session.save(book) ;
    tr.commit() ;
    session.close() ;
  }

  //删除(根据ID)
  public void delBookById(String bookId) {
    Session session=getSession();
    Transaction tr=session.beginTransaction();
    String hql="delete Book as book where book.bookId=?";
    Query quer=session.createQuery(hql);
    quer.setString(0,bookId) ;
    quer.executeUpdate() ;
    tr.commit() ;
    session.close();
  }
  //删除(批量删除)
  public void delBooks(String[] bookIds) {
    Session session=getSession();
    Transaction tr=session.beginTransaction();
    String hql="delete Book as book where book.bookId=?";

    for(int i=0;i<bookIds.length ;i++){
       (session.createQuery(hql)).setString(0,bookIds[i]).executeUpdate() ;
    }
    tr.commit() ;
    session.close() ;
  }

  //修改
  public void updateBook(Book book) {
    Session session=getSession();
    Transaction tr=session.beginTransaction();
    session.update(book) ;
    tr.commit() ;
    session.close();
  }

  //查找(根据ID)
  public Book findBookById(String bookId) {
    Session session=getSession();
    Transaction tr=session.beginTransaction() ;
    String hql="select book from Book as book where book.bookId=?";
    Query quer=session.createQuery(hql) ;
    quer.setString(0,bookId) ;

    Book book=null;
    List list=quer.list() ;

    tr.commit() ;
    session.close() ;

    if(list.size() >0)//可防止空指针的
    book=(Book)list.get(0) ;
    return book;
  }

//总记录数
  public int getBookCounts() {
    Session session=getSession();
    Transaction tr=session.beginTransaction() ;
    String hql="select count(book.bookId) from Book as book";
    Query quer=session.createQuery(hql);
    int count=((Integer)quer.iterate().next()).intValue() ;
    return count;
  }

  //查找全部书
  public Map findBooks(int startRow, int maxRows) {
    Session session=getSession();
    Transaction tr=session.beginTransaction() ;
    Map map=new HashMap();

    String hql="from Book";
    Query quer=session.createQuery(hql);
    quer.setFirstResult(startRow);
    quer.setMaxResults(maxRows) ;

    List list=quer.list() ;
    Iterator it=list.iterator() ;
    while(it.hasNext()){
        Book book=(Book)it.next();
        map.put(book.getBookId(),book) ;
    }
    tr.commit() ;
    session.close() ;
    return map;
  }

//获得当前系统时间,转换成yyyy-MM-dd格式,准备入库
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date pdate = Date.valueOf(sdf.format(cal.getTime()));
//上传文件
 UploadForm uploadForm = (UploadForm) form;
    FormFile file = uploadForm.getFile();

    String filename = file.getFileName();

    //设置默认文件存储目录
    String path = "d://upload//pic//";

    //文件路径
    String filepath = path + filename;

    try {
      //上传文件写到本地文件系统
      InputStream is = file.getInputStream();
      FileTool.writeFile(filepath, is);
    }
    catch (FileNotFoundException ex) {
    }
    catch (IOException ex) {
    }

//
ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response
//数据库连接
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/tarena", "root","weblogic");
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from t_picture where id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, pid);
rs = ps.executeQuery(); 

public boolean addPicture(PictureBean pb) {
    //上传数据入库
    boolean result = false;
    String sql = "insert into t_picture(title,author,type,area,pdate,filepath) values (?,?,?,?,?,?)";

    Connection con = DBConnector.getConnection();
    PreparedStatement ps = null;

    try {
      ps = con.prepareStatement(sql);
      int i = 1;
      ps.setString(i++, pb.getTitle());
      ps.setString(i++, pb.getAuthor());
      ps.setString(i++, pb.getType());
      ps.setString(i++, pb.getArea());
      ps.setDate(i++, pb.getPdate());
      ps.setString(i++, pb.getFilepath());
      int j = ps.executeUpdate();
      if (j > 0) {
        result = true;
      }
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    }
    finally {
      close(null, ps, con);
    }
    return result;
  }

  public PictureBean queryPicture(int pid) {
    //查询某类型图片摘要信息
    PictureBean pb = null;
    String sql = "select * from t_picture where id=?";
    Connection con = DBConnector.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      ps = con.prepareStatement(sql);
      ps.setInt(1, pid);
      rs = ps.executeQuery();
      if (rs.next()) {
        pb = new PictureBean();
        pb.setId(rs.getInt("id"));
        pb.setTitle(rs.getString("title"));
        pb.setAuthor(rs.getString("author"));
        pb.setType(rs.getString("type"));
        pb.setArea(rs.getString("area"));
        pb.setPdate(rs.getDate("pdate"));
        pb.setFilepath(rs.getString("filepath"));
      }
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    }
    finally {
      close(rs, ps, con);
    }

    return pb;
  }

  public Collection queryByType(String type) {
    //查询某张图片详细信息
    Vector pbs = new Vector();
    String sql = "select * from t_picture where type=?";
    Connection con = DBConnector.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      ps = con.prepareStatement(sql);
      ps.setString(1, type);
      rs = ps.executeQuery();
      while (rs.next()) {
        PictureBean pb = new PictureBean();
        pb.setId(rs.getInt("id"));
        pb.setTitle(rs.getString("title"));
        pb.setAuthor(rs.getString("author"));
        pb.setPdate(rs.getDate("pdate"));
        pbs.add(pb);
      }
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    }
    finally {
      close(rs, ps, con);
    }

    return pbs;

  }

 

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

beauty9235

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值