op_book.java~17~
上传用户:tanyanyong
上传日期:2013-06-23
资源大小:1355k
文件大小:18k
源码类别:

电子政务应用

开发平台:

MultiPlatform

  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 = "";
  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.getLeav_number() + "',";
  269.                 sqlStr = sqlStr + "now()"+ "'";
  270.                 sqlStr = sqlStr + abooks.getPicture()+"'";
  271.                 try
  272.                 {       DataBase db = new DataBase();
  273.                         db.connect();
  274.                         stmt =db.conn.createStatement ();
  275.                         stmt.execute(sqlStr);
  276.                         return true;
  277.                 }
  278.                 catch (SQLException sqle)
  279.                 {
  280.                         return false;
  281.                 }
  282.         }
  283.         public boolean update() throws Exception {
  284.                 sqlStr = "update book set ";
  285.                 sqlStr = sqlStr + "bookname = '" + dataFormat.toSql(abooks.getBookName()) + "',";
  286.                 sqlStr = sqlStr + "bookclass = '" + abooks.getBookClass() + "',";
  287.                 sqlStr = sqlStr + "Author = '" + dataFormat.toSql(abooks.getAuthor()) + "',";
  288.                 sqlStr = sqlStr + "publish = '" + dataFormat.toSql(abooks.getPublish()) + "',";
  289.                 sqlStr = sqlStr + "bookno = '" + dataFormat.toSql(abooks.getBookNo()) + "',";
  290.                 sqlStr = sqlStr + "content = '" + dataFormat.toSql(abooks.getContent()) + "',";
  291.                 sqlStr = sqlStr + "prince = '" + abooks.getPrince() + "',";
  292.                 sqlStr = sqlStr + "Amount = '" + abooks.getAmount() + "',";
  293.                 sqlStr = sqlStr + "leav_number = '" + abooks.getLeav_number()+ "' ";
  294.                 sqlStr = sqlStr + "picture = '" + abooks.getPicture() + "' ";
  295.                 sqlStr = sqlStr + "where id = '" + abooks.getId() + "'";
  296.                 System.out.println(sqlStr);
  297.                 try
  298.                 {
  299.                         DataBase db = new DataBase();
  300.                         db.connect();
  301.                         stmt =db.conn.createStatement ();
  302.                         stmt.execute(sqlStr);
  303.                         return true;
  304.                 }
  305.                 catch (SQLException e)
  306.                 {        System.out.print(e.getMessage());
  307.                         return false;
  308.                 }
  309.         }
  310.         public boolean delete( int aid ) throws Exception {
  311.                 sqlStr = "delete from book where id = "  + aid ;
  312.                 try
  313.                 {         DataBase db = new DataBase();
  314.                            db.connect();
  315.                            stmt =db.conn.createStatement ();
  316.                         stmt.execute(sqlStr);
  317.                         return true;
  318.                 }
  319.                 catch (SQLException e)
  320.                 {
  321.                         System.out.println(e);
  322.                         return false;
  323.                 }
  324.         }
  325.         public boolean getOnebook(int newid ) throws Exception {
  326.           DataBase db = new DataBase();
  327.                            db.connect();
  328.                            stmt =db.conn.createStatement ();
  329.                 try
  330.                 {
  331.                         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 ;
  332.                         rs = stmt.executeQuery(sqlStr);
  333.                         if (rs.next())
  334.                         { booklist = new Vector(1);
  335.                                 book book = new book();
  336.                                 book.setId(rs.getLong("id"));
  337.                                 book.setBookName(rs.getString("bookname"));
  338.                                 book.setBookClass(rs.getInt("bookclass"));
  339.                                 book.setClassname(rs.getString("classname"));
  340.                                 book.setAuthor(rs.getString("author"));
  341.                                 book.setPublish(rs.getString("publish"));
  342.                                 book.setBookNo(rs.getString("Bookno"));
  343.                                 book.setContent(rs.getString("content"));
  344.                                 book.setPrince(rs.getFloat("prince"));
  345.                                 book.setAmount(rs.getInt("amount"));
  346.                                 book.setLeav_number(rs.getInt("leav_number"));
  347.                                 book.setRegTime(rs.getString("regtime"));
  348.                                 book.setPicture(rs.getString("picture"));
  349.                                 booklist.addElement(book);
  350.                         } else {
  351.                                 rs.close();
  352.                                 return false;
  353.                         }
  354.                         rs.close();
  355.                         return true;
  356.                 }
  357.                 catch (SQLException e)
  358.                 {
  359.                         return false;
  360.                 }
  361.         }
  362.         public int getPage() { //显示的页码
  363.                 return page;
  364.         }
  365.         public void setPage(int newpage) {
  366.                 page = newpage;
  367.         }
  368.         public int getPageSize(){ //每页显示的图书数
  369.                 return pageSize;
  370.         }
  371.         public void setPageSize(int newpsize) {
  372.                 pageSize = newpsize;
  373.         }
  374.         public int getPageCount() { //页面总数
  375.                 return pageCount;
  376.         }
  377.         public void setPageCount(int newpcount) {
  378.                 pageCount = newpcount;
  379.         }
  380.         public long getRecordCount() {
  381.                 return recordCount;
  382.         }
  383.         public void setRecordCount(long newrcount) {
  384.                 recordCount= newrcount;
  385.         }
  386.   public op_book() {
  387.   }
  388. }