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

数据库编程

开发平台:

Java

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