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
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值