op_book.java
上传用户:sxlinghang
上传日期:2022-07-20
资源大小:1405k
文件大小:15k
源码类别:

数据库编程

开发平台:

Java

  1. package bookshop.run;
  2. /**
  3.  * <p>管理图书的类,包括图书的修改、查询,删除,添加 </p>
  4.  */
  5. import java.sql.*;
  6. import java.util.Vector;
  7. import bookshop.util.*;
  8. import bookshop.book.*;
  9. import javax.servlet.http.HttpServletRequest;
  10. public class op_book extends DataBase{
  11.   private book abooks = new book();                        //新的图书类
  12.         private javax.servlet.http.HttpServletRequest request; //建立页面请求
  13.         private boolean sqlflag = true ;     //对接收到的数据是否正确
  14.         private Vector booklist;     //显示图书列表向量数组
  15.         private int page = 1;        //显示的页码
  16.         private int pageSize=10; //每页显示的图书数
  17.         private int pageCount =0; //页面总数
  18.         private long recordCount =0;         //查询的记录总数
  19.         public String sqlStr="";
  20.         public Vector getBooklist() {
  21.                 return booklist;
  22.         }
  23.         public boolean getSqlflag() {
  24.                 return sqlflag;
  25.         }
  26.         public String to_String( String str) {
  27.                 try
  28.                 {
  29.                         return new String(str.getBytes("ISO8859-1"));
  30.                 }
  31.                 catch (Exception e)
  32.                 {
  33.                         return str;
  34.                 }
  35.         }
  36.         /**
  37.          * 将页面表单传来的资料分解
  38.          */
  39. public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
  40.   boolean flag = false;
  41.   try{
  42.    request = newrequest;
  43.    String ID = request.getParameter("id");
  44.    long bookid = 0;
  45.    try{
  46.            bookid = Long.parseLong(ID);
  47.    }catch (Exception e){
  48.    }
  49.    abooks.setId(bookid);
  50.    String bookname = request.getParameter("bookname");
  51.    if (bookname==null || bookname.equals(""))
  52.    {
  53.            bookname = "";
  54.            sqlflag = false;
  55.    }
  56.    abooks.setBookName(to_String(bookname));
  57.    String author = request.getParameter("author");
  58.    if (author==null || author.equals(""))
  59.    {
  60.            author = "";
  61.            sqlflag = false;
  62.    }
  63.    abooks.setAuthor(to_String(author));
  64.    String publish = request.getParameter("publish");;
  65.    if (publish==null)
  66.    {
  67.            publish = "";
  68.    }
  69.    abooks.setPublish(to_String(publish));
  70.    String bookclass = request.getParameter("bookclass");
  71.    int bc = Integer.parseInt(bookclass);
  72.    abooks.setBookClass(bc);
  73.    String bookno = request.getParameter("bookno");
  74.    if (bookno == null)
  75.    {
  76.            bookno = "";
  77.    }
  78.    abooks.setBookNo(to_String(bookno));
  79.    String picture = request.getParameter("picture");
  80.    if (picture == null)
  81.    {
  82.            picture = "images/01.gif";
  83.    }
  84.    abooks.setPicture(to_String(picture));
  85.    float price;
  86.    try {
  87.            price =new Float(request.getParameter("price")).floatValue();
  88.    } catch (Exception e){
  89.            price = 0;
  90.            sqlflag = false;
  91.    }
  92.    abooks.setPrince(price);
  93.    int amount;
  94.    try{
  95.            amount = new Integer(request.getParameter("amount")).intValue();
  96.    }catch (Exception e){
  97.            sqlflag = false;
  98.            amount = 0;
  99.    }
  100.    abooks.setAmount(amount);
  101.    String content = request.getParameter("content");
  102.    if (content == null)
  103.    {
  104.            content = "";
  105.    }
  106.    abooks.setContent(to_String(content));
  107.    if (sqlflag)
  108.    {
  109.            flag = true;
  110.    }
  111.    return flag;
  112.                 }catch (Exception e){
  113.    return flag;
  114.                 }
  115.         }
  116.         /**
  117.          * 获得查询图书类别的sql语句
  118.          * @return
  119.          */
  120. public String getSql() {
  121.    sqlStr = "select id,classname from book order by id";
  122.    return sqlStr;
  123.         }
  124.         /**
  125.          * 完成图书查询,包括分类,分页查询
  126.          * @param res
  127.          * @return
  128.          * @throws java.lang.Exception
  129.          */
  130. public boolean book_search(HttpServletRequest res) throws Exception {
  131.    DataBase db = new DataBase();
  132.    db.connect();
  133.    stmt = db.conn.createStatement ();
  134.    request = res;
  135.    String PAGE = request.getParameter("page");   //页码
  136.    String classid = request.getParameter("classid"); //分类ID号
  137.    String keyword = request.getParameter("keyword"); //查询关键词
  138.    if (classid==null) classid="";
  139.    if (keyword==null) keyword = "";
  140.    keyword = to_String(keyword).toUpperCase();
  141.    try {
  142.            page = Integer.parseInt(PAGE);
  143.    }catch (NumberFormatException e){
  144.            page = 1;
  145.    }
  146.    //取出记录数
  147.    if (!classid.equals("") && keyword.equals("") ) {
  148.            sqlStr = "select count(*) from book where bookclass='"+classid + "'";
  149.       }
  150.   else if (!keyword.equals("")) {
  151.       if (classid.equals("")){
  152.            sqlStr = "select count(*) from book where upper(bookname) like '%" +
  153.                     keyword+ "%' or upper(content) like '%" + keyword + "%'";
  154.       } else {
  155.           sqlStr = "select count(*) from book where bookclass='" + classid
  156.                    + "' and  (upper(bookname) like '%" +keyword+ "%' or "+
  157.                    "upper(content) like '%" + keyword + "%')";
  158.            }
  159.    } else {
  160.            sqlStr = "select count(*) from book";
  161.           }
  162.    int rscount = pageSize;
  163.    try {
  164.            ResultSet rs1 = stmt.executeQuery(sqlStr);
  165.            if (rs1.next()) recordCount = rs1.getInt(1);
  166.            rs1.close();
  167.    }catch (SQLException e){
  168.            System.out.println(e.getMessage());
  169.            return false;
  170.    }
  171.    //设定有多少pageCount
  172.    if (recordCount < 1)
  173.             pageCount = 0;
  174.    else
  175.             pageCount = (int)(recordCount - 1) / pageSize + 1;
  176.    //检查查看的页面数是否在范围内
  177.    if (page < 1)
  178.             page = 1;
  179.   else if (page > pageCount)
  180.             page = pageCount;
  181.             rscount = (int) recordCount % pageSize;  // 最后一页记录数
  182.    //sql为倒序取值
  183.    sqlStr = "select   a.id,a.bookname,a.bookclass,b.classname,"+
  184.            "a.author,a.publish,a.bookno,a.content,a.prince,a.amount,"+
  185.            "a.Leav_number,a.regtime,a.picture from book a,bookclass b"+
  186.           " where a.Bookclass = b.Id ";
  187.   if (!classid.equals("") && keyword.equals("") ){  //如果类别不为空,非查询
  188.            if (page == 1)
  189.            {
  190.                sqlStr = sqlStr + " and a.bookclass='" + classid + "' "+
  191.                "order by a.Id desc";
  192.            } else {
  193.                sqlStr = sqlStr + " and a.bookclass='" + classid + "limit "+
  194.                        (recordCount-pageSize * page)+","+(recordCount-pageSize * (page-1));
  195.            }
  196.   } else if (!keyword.equals("")) {  //如果是查询资料
  197.     if (page == 1){
  198.       if (!classid.equals("")) {//查询某一类
  199.          sqlStr = sqlStr + "and a.Bookclass='" +
  200.          classid + "' and (upper(a.bookname) like '%" +
  201.          keyword+ "%' or upper(a.content) like '%" +
  202.          keyword + "%')  order by a.Id desc";
  203.       } else { //查询所有类
  204.         sqlStr = sqlStr + " and (upper(a.bookname) like '%" +
  205.         keyword+ "%' or upper(a.content) like '%" +
  206.         keyword + "%') order by a.Id desc";
  207.       }
  208.      } else {
  209.       if (!classid.equals("")){
  210.            sqlStr = sqlStr + " and a.Bookclass='" +
  211.            classid + "' and (upper(a.bookname) like '%" +
  212.            keyword+ "%' or upper(a.content) like '%" +
  213.            keyword + "%') limit "+(recordCount-pageSize * page)+","+
  214.            (recordCount-pageSize * (page-1));
  215.       } else {
  216.             sqlStr = sqlStr + " and (upper(a.bookname) like '%" +
  217.             keyword+ "%' or upper(a.content) like '%" +
  218.             keyword + "%') limit "+(recordCount-pageSize * page)+","+
  219.             (recordCount-pageSize * (page-1));
  220.             }
  221.                         }
  222.   } else {//非查询,也非分类浏览
  223.          if (page == 1){
  224.               sqlStr = sqlStr + "  order by a.Id desc limit 0,"+pageSize;
  225.           } else {
  226.               sqlStr = sqlStr + "limit "+(recordCount-pageSize * page)+","+
  227.               (recordCount-pageSize * (page-1));
  228.            }
  229.         }
  230.         try  {
  231.               rs = stmt.executeQuery(sqlStr);
  232.               booklist = new Vector(rscount);
  233.               while (rs.next()){
  234.                                 book book = new book();
  235.                                 book.setId(rs.getLong("id"));
  236.                                 book.setBookName(rs.getString("bookname"));
  237.                                 book.setBookClass(rs.getInt("bookclass"));
  238.                                 book.setClassname(rs.getString("classname"));
  239.                                 book.setAuthor(rs.getString("author"));
  240.                                 book.setPublish(rs.getString("publish"));
  241.                                 book.setBookNo(rs.getString("Bookno"));
  242.                                 book.setContent(rs.getString("content"));
  243.                                 book.setPrince(rs.getFloat("prince"));
  244.                                 book.setAmount(rs.getInt("amount"));
  245.                                 book.setLeav_number(rs.getInt("leav_number"));
  246.                                 book.setRegTime(rs.getString("regtime"));
  247.                                 book.setPicture(rs.getString("picture"));
  248.                                 booklist.addElement(book);
  249.                         }
  250.                         rs.close();
  251.                         return true;
  252.                 }catch (Exception e){
  253.                         System.out.println(e.getMessage());
  254.                         return false;
  255.                 }
  256.         }
  257.         /**
  258.          * 完成图书添加
  259.          * @return
  260.          * @throws java.lang.Exception
  261.          */
  262.  public boolean insert() throws Exception {
  263.   sqlStr = "insert into book (Bookname,Bookclass,Author,Publish,Bookno,"+
  264.            "Content,Prince,Amount,Leav_number,Regtime,picture) values ('";
  265.            sqlStr = sqlStr + dataFormat.toSql(abooks.getBookName()) + "','";
  266.            sqlStr = sqlStr + abooks.getBookClass() + "','";
  267.            sqlStr = sqlStr + dataFormat.toSql(abooks.getAuthor()) + "','";
  268.            sqlStr = sqlStr + dataFormat.toSql(abooks.getPublish()) + "','";
  269.            sqlStr = sqlStr + dataFormat.toSql(abooks.getBookNo()) + "','";
  270.            sqlStr = sqlStr + dataFormat.toSql(abooks.getContent()) + "','";
  271.            sqlStr = sqlStr + abooks.getPrince() + "','";
  272.            sqlStr = sqlStr + abooks.getAmount() + "','";
  273.            sqlStr = sqlStr + abooks.getAmount() + "',";
  274.            sqlStr = sqlStr + "now()"+ ",'";
  275.            sqlStr = sqlStr + abooks.getPicture()+"')";
  276.                 try{
  277.                         System.out.print(sqlStr);
  278.                         DataBase db = new DataBase();
  279.                         db.connect();
  280.                         stmt =db.conn.createStatement ();
  281.                         stmt.execute(sqlStr);
  282.                         return true;
  283.                 }catch (SQLException sqle){
  284.                   System.out.print(sqle.getMessage());
  285.                         return false;
  286.                 }
  287.         }
  288.         /**
  289.          * 完成图书修改
  290.          * @return
  291.          * @throws java.lang.Exception
  292.          */
  293. public boolean update() throws Exception {
  294.   sqlStr = "update book set ";
  295.   sqlStr = sqlStr + "bookname = '" + dataFormat.toSql(abooks.getBookName()) + "',";
  296.   sqlStr = sqlStr + "bookclass = '" + abooks.getBookClass() + "',";
  297.   sqlStr = sqlStr + "Author = '" + dataFormat.toSql(abooks.getAuthor()) + "',";
  298.   sqlStr = sqlStr + "publish = '" + dataFormat.toSql(abooks.getPublish()) + "',";
  299.   sqlStr = sqlStr + "bookno = '" + dataFormat.toSql(abooks.getBookNo()) + "',";
  300.   sqlStr = sqlStr + "content = '" + dataFormat.toSql(abooks.getContent()) + "',";
  301.   sqlStr = sqlStr + "prince = '" + abooks.getPrince() + "',";
  302.   sqlStr = sqlStr + "Amount = '" + abooks.getAmount() + "',";
  303.   sqlStr = sqlStr + "leav_number = '" + abooks.getLeav_number()+ "' ,";
  304.   sqlStr = sqlStr + "picture = '" + abooks.getPicture() + "' ";
  305.   sqlStr = sqlStr + "where id = " + abooks.getId();
  306.                 try{
  307.                         DataBase db = new DataBase();
  308.                         db.connect();
  309.                         stmt =db.conn.createStatement ();
  310.                         stmt.execute(sqlStr);
  311.                         return true;
  312.                 } catch (SQLException e){
  313.                         System.out.print(e.getMessage());
  314.                         return false;
  315.                 }
  316.         }
  317.         /**
  318.          * 完成图书删除
  319.          * @param aid
  320.          * @return
  321.          * @throws java.lang.Exception
  322.          */
  323.         public boolean delete( int aid ) throws Exception {
  324.                 sqlStr = "delete from book where id = "  + aid ;
  325.                 try
  326.                 {         DataBase db = new DataBase();
  327.                           db.connect();
  328.                           stmt =db.conn.createStatement ();
  329.                           stmt.execute(sqlStr);
  330.                          return true;
  331.                 }
  332.                 catch (SQLException e)
  333.                 {
  334.                         System.out.println(e);
  335.                         return false;
  336.                 }
  337.               }
  338.               /**
  339.                * 完成图书单本查询,用于支持页面的查看图书详细资料
  340.                * @param newid
  341.                * @return
  342.                * @throws java.lang.Exception
  343.                */
  344.  public boolean getOnebook(int newid ) throws Exception {
  345.      DataBase db = new DataBase();
  346.      db.connect();
  347.      stmt =db.conn.createStatement ();
  348.      try {
  349.         sqlStr="select  a.id,a.bookname,a.bookclass,b.classname,a.author,"+
  350.         "a.publish,a.bookno,a.content,a.prince,a.amount,a.Leav_number,"+
  351.         "a.regtime,a.picture from book a,bookclass b where a.Bookclass="+
  352.         "b.Id and a.Id = " + newid ;
  353.                         rs = stmt.executeQuery(sqlStr);
  354.                         if (rs.next())
  355.                         { booklist = new Vector(1);
  356.                                 book book = new book();
  357.                                 book.setId(rs.getLong("id"));
  358.                                 book.setBookName(rs.getString("bookname"));
  359.                                 book.setBookClass(rs.getInt("bookclass"));
  360.                                 book.setClassname(rs.getString("classname"));
  361.                                 book.setAuthor(rs.getString("author"));
  362.                                 book.setPublish(rs.getString("publish"));
  363.                                 book.setBookNo(rs.getString("Bookno"));
  364.                                 book.setContent(rs.getString("content"));
  365.                                 book.setPrince(rs.getFloat("prince"));
  366.                                 book.setAmount(rs.getInt("amount"));
  367.                                 book.setLeav_number(rs.getInt("leav_number"));
  368.                                 book.setRegTime(rs.getString("regtime"));
  369.                                 book.setPicture(rs.getString("picture"));
  370.                                 booklist.addElement(book);
  371.                         } else {
  372.                                 rs.close();
  373.                                 return false;
  374.                         }
  375.                         rs.close();
  376.                         return true;
  377.                 }
  378.                 catch (SQLException e)
  379.                 {
  380.                         return false;
  381.                 }
  382.         }
  383.         public int getPage() { //显示的页码
  384.                 return page;
  385.         }
  386.         public void setPage(int newpage) {
  387.                 page = newpage;
  388.         }
  389.         public int getPageSize(){ //每页显示的图书数
  390.                 return pageSize;
  391.         }
  392.         public void setPageSize(int newpsize) {
  393.                 pageSize = newpsize;
  394.         }
  395.         public int getPageCount() { //页面总数
  396.                 return pageCount;
  397.         }
  398.         public void setPageCount(int newpcount) {
  399.                 pageCount = newpcount;
  400.         }
  401.         public long getRecordCount() {
  402.                 return recordCount;
  403.         }
  404.         public void setRecordCount(long newrcount) {
  405.                 recordCount= newrcount;
  406.         }
  407.   public op_book() {
  408.        }
  409. }