⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 op_book.java~97~

📁 基于JSP的网上书店.功能较为全面 开发环境:sql server2000+tomcat+jdk
💻 JAVA~97~
字号:
package bookshop.run;
/**
 * <p>管理图书的类,包括图书的修改、查询,删除,添加 </p>
 */
import java.sql.*;
import java.util.Vector;
import bookshop.util.*;
import bookshop.book.*;
import javax.servlet.http.HttpServletRequest;

public class op_book //extends DataBase{
{
  private book abooks = new book();	                       //新的图书类
        private javax.servlet.http.HttpServletRequest request; //建立页面请求
        private boolean sqlflag = true ;		    //对接收到的数据是否正确
        private Vector booklist;			    //显示图书列表向量数组
        private int page = 1;				       //显示的页码
        private int pageSize=3;				//每页显示的图书数
        private int pageCount =0;				//页面总数
        private long recordCount =0;			        //查询的记录总数
        public String sqlStr="";
        private ResultSet rs;

        public Vector getBooklist() {
                return booklist;
        }
        public boolean getSqlflag() {
                return sqlflag;
        }
        public String to_String( String str) {
                try
                {
                        return new String(str.getBytes("ISO8859-1"));
                }
                catch (Exception e)
                {
                        return str;
                }
        }
        public void finalize()
        {
          //super.finalize();
        }

        /**
         * 将页面表单传来的资料分解
         */
public boolean getRequest(javax.servlet.http.HttpServletRequest newrequest) {
  boolean flag = false;
  try{
   request = newrequest;
   String ID = request.getParameter("id");
   long bookid = 0;
   try{
           bookid = Long.parseLong(ID);
   }catch (Exception e){
   }
   abooks.setId(bookid);
   String bookname = request.getParameter("bookname");
   if (bookname==null || bookname.equals(""))
   {
           bookname = "";
           sqlflag = false;
   }
   abooks.setBookName((bookname));
   String author = request.getParameter("author");
   if (author==null || author.equals(""))
   {
           author = "";
           sqlflag = false;
   }
   abooks.setAuthor((author));
   String publish = request.getParameter("publish");;
   if (publish==null)
   {
           publish = "";
   }
   abooks.setPublish((publish));
   String bookclass = request.getParameter("bookclass");
   int bc = Integer.parseInt(bookclass);
   abooks.setBookClass(bc);
   String bookno = request.getParameter("bookno");
   if (bookno == null)
   {
           bookno = "";
   }
   abooks.setBookNo((bookno));
   String picture = request.getParameter("picture");
   if (picture == null)
   {
           picture = "images/01.gif";
   }
   abooks.setPicture((picture));
   float price;
   try	{
           price =new Float(request.getParameter("price")).floatValue();
   } catch (Exception e){
           price = 0;
           sqlflag = false;
   }
   abooks.setPrince(price);
   int amount;
   try{
           amount = new Integer(request.getParameter("amount")).intValue();
   }catch (Exception e){
           sqlflag = false;
           amount = 0;
   }
   abooks.setAmount(amount);

   //int amount;
   try{
           amount = new Integer(request.getParameter("leavenum")).intValue();
   }catch (Exception e){
           //sqlflag = false;
           amount = 0;
   }
   abooks.setLeav_number(amount);

   String content = request.getParameter("content");
   if (content == null)
   {
           content = "";
   }
   abooks.setContent((content));
   if (sqlflag)
   {
           flag = true;
   }
   return flag;
                }catch (Exception e){
   return flag;
                }
        }
        /**
         * 获得查询图书类别的sql语句
         * @return
         */
public String getSql() {
   sqlStr = "select id,classname from book order by id";
   return sqlStr;
        }
        /**
         * 完成图书查询,包括分类,分页查询
         * @param res
         * @return
         * @throws java.lang.Exception
         */
public boolean book_search(HttpServletRequest res) throws Exception {
   DataBase db = new DataBase();
   //db.connect();
   //stmt = db.conn.createStatement ();
   request = res;
   String PAGE = request.getParameter("page");   //页码
   String classid = request.getParameter("classid");	//分类ID号
   String keyword = request.getParameter("keyword");	//查询关键词

   keyword = dataFormat.toSql(keyword);

   if (classid==null) classid="";
   if (keyword==null) keyword = "";
   keyword = (keyword).toUpperCase();
   try {
           page = Integer.parseInt(PAGE);
   }catch (NumberFormatException e){
           page = 1;
   }
   //取出记录数
   sqlStr = "SELECT COUNT(*) FROM book WHERE 1=1 ";// 注意这里1=1的妙用
   if( !classid.equals("") )
   {
     sqlStr = sqlStr + " AND bookclass='"+classid + "'";
   }
   if (!keyword.equals(""))
   {
     sqlStr = sqlStr + " AND  (UPPER(bookname) LIKE '%" +keyword+ "%' OR "+
                   "UPPER(content) LIKE '%" + keyword + "%')";
   }

   int rscount = pageSize;
   try {
           ResultSet rs1 = db.stmt.executeQuery(sqlStr);
           if (rs1.next()) recordCount = rs1.getInt(1);
           rs1.close();
   }catch (SQLException e){
           System.out.println(e.getMessage());
           return false;
   }
   //设定有多少pageCount
   if (recordCount < 1)
   {
     pageCount = 0;
   }
   else
   {
     pageCount = (int) (recordCount - 1) / pageSize + 1;
   }
   //检查查看的页面数是否在范围内
   if (page < 1)
   {
     page = 1;
   }
   else if (page > pageCount)
   {
     page = pageCount;
   }
   rscount = (int) recordCount % pageSize;	 // 最后一页记录数
   //sql为倒序取值
   sqlStr = "SELECT TOP " + (recordCount-pageSize * (page-1)) ;
   sqlStr = sqlStr + " 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 ";
   if( !classid.equals("") )  //如果类别不为空
   {
     sqlStr = sqlStr + " AND a.bookclass='" + classid + "' ";
   }
   if( !keyword.equals("" ) )  //如果关键字不为空
   {
     sqlStr = sqlStr + " AND  (UPPER(bookname) LIKE '%" +keyword+ "%' OR "+
                  "UPPER(content) LIKE '%" + keyword + "%') ";
   }
   sqlStr = sqlStr + " ORDER BY a.Id ASC ";
   sqlStr = " SELECT TOP " + pageSize +  " * FROM ( " +  sqlStr + " ) myTable ORDER BY a.Id DESC " ;

    try  {
     System.out.println(sqlStr);
     rs = db.stmt.executeQuery(sqlStr);
     booklist = new Vector(rscount);
     while (rs.next()){
       book book = new book();
       book.setId(rs.getLong("id"));
       book.setBookName(rs.getString("bookname"));
       book.setBookClass(rs.getInt("bookclass"));
       book.setClassname(rs.getString("classname"));
       book.setAuthor(rs.getString("author"));
       book.setPublish(rs.getString("publish"));
       book.setBookNo(rs.getString("bookno"));
       book.setContent(rs.getString("content"));
       book.setPrince(rs.getFloat("prince"));
       book.setAmount(rs.getInt("amount"));
       book.setLeav_number(rs.getInt("leav_number"));
       book.setRegTime(rs.getString("regtime"));
       book.setPicture(rs.getString("picture"));
       booklist.addElement(book);
     }
     rs.close();
     db.finalize();
     return true;
   }catch (Exception e){
     System.out.println(e.getMessage());
     db.finalize();
     return false;
   }
 }

 /**
  * 完成图书添加
  * @return
  * @throws java.lang.Exception
  */
 public boolean insert() throws Exception {
  sqlStr = "insert into book (Bookname,Bookclass,Author,Publish,Bookno,"+
           "Content,Prince,Amount,Leav_number,Regtime,picture) values ('";
           sqlStr = sqlStr + dataFormat.toSql(abooks.getBookName()) + "','";
           sqlStr = sqlStr + abooks.getBookClass() + "','";
           sqlStr = sqlStr + dataFormat.toSql(abooks.getAuthor()) + "','";
           sqlStr = sqlStr + dataFormat.toSql(abooks.getPublish()) + "','";
           sqlStr = sqlStr + dataFormat.toSql(abooks.getBookNo()) + "','";
           sqlStr = sqlStr + dataFormat.toSql(abooks.getContent()) + "','";
           sqlStr = sqlStr + abooks.getPrince() + "','";
           sqlStr = sqlStr + abooks.getAmount() + "','";
           sqlStr = sqlStr + abooks.getAmount() + "',";
           sqlStr = sqlStr + "GETDATE()"+ ",'";
           sqlStr = sqlStr + abooks.getPicture()+"')";
                try{
                        System.out.print(sqlStr);
                        DataBase db = new DataBase();
                        //db.connect();
                        //stmt =db.conn.createStatement ();
                        db.stmt.execute(sqlStr);
                        db.finalize();
                        return true;
                }catch (SQLException sqle){
                  System.out.print(sqle.getMessage());
                        return false;
                }
        }
        /**
         * 完成图书修改
         * @return
         * @throws java.lang.Exception
         */
public boolean update() throws Exception {
  sqlStr = "update book set ";
  sqlStr = sqlStr + "bookname = '" + dataFormat.toSql(abooks.getBookName()) + "',";
  sqlStr = sqlStr + "bookclass = '" + abooks.getBookClass() + "',";
  sqlStr = sqlStr + "Author = '" + dataFormat.toSql(abooks.getAuthor()) + "',";
  sqlStr = sqlStr + "publish = '" + dataFormat.toSql(abooks.getPublish()) + "',";
  sqlStr = sqlStr + "bookno = '" + dataFormat.toSql(abooks.getBookNo()) + "',";
  sqlStr = sqlStr + "content = '" + dataFormat.toSql(abooks.getContent()) + "',";
  sqlStr = sqlStr + "prince = '" + abooks.getPrince() + "',";
  sqlStr = sqlStr + "Amount = '" + abooks.getAmount() + "',";
  sqlStr = sqlStr + "leav_number = '" + abooks.getLeav_number()+ "' ,";
  sqlStr = sqlStr + "picture = '" + abooks.getPicture() + "' ";
  sqlStr = sqlStr + "where id = " + abooks.getId();
                try{
                        DataBase db = new DataBase();
                        //db.connect();
                        //stmt =db.conn.createStatement ();
                        db.stmt.execute(sqlStr);
                        db.finalize();
                        return true;
                } catch (SQLException e){
                        System.out.print(e.getMessage());
                        return false;
                }

        }
        /**
         * 完成图书删除
         * @param aid
         * @return
         * @throws java.lang.Exception
         */
        public boolean delete( int aid ) throws Exception {
                sqlStr = "delete from book where id = "  + aid ;
                try
                {         DataBase db = new DataBase();
                          //db.connect();
                          //stmt =db.conn.createStatement ();
                          db.stmt.execute(sqlStr);
                          db.finalize();
                         return true;
                }
                catch (SQLException e)
                {
                        System.out.println(e);
                        return false;
                }
              }
              /**
               * 完成图书单本查询,用于支持页面的查看图书详细资料
               * @param newid
               * @return
               * @throws java.lang.Exception
               */
 public boolean getOnebook(int newid ) throws Exception {
     DataBase db = new DataBase();
     //db.connect();
     //stmt =db.conn.createStatement ();
     try {
        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 ;
                        rs = db.stmt.executeQuery(sqlStr);
                        if (rs.next())
                        {	booklist = new Vector(1);
                                book book = new book();
                                book.setId(rs.getLong("id"));
                                book.setBookName(rs.getString("bookname"));
                                book.setBookClass(rs.getInt("bookclass"));
                                book.setClassname(rs.getString("classname"));
                                book.setAuthor(rs.getString("author"));
                                book.setPublish(rs.getString("publish"));
                                book.setBookNo(rs.getString("bookno"));
                                book.setContent(rs.getString("content"));
                                book.setPrince(rs.getFloat("prince"));
                                book.setAmount(rs.getInt("amount"));
                                book.setLeav_number(rs.getInt("leav_number"));
                                book.setRegTime(rs.getString("regtime"));
                                book.setPicture(rs.getString("picture"));
                                booklist.addElement(book);
                        } else {
                                rs.close();
                                return false;
                        }
                        rs.close();
                        db.finalize();
                        return true;
                }
                catch (SQLException e)
                {
                        return false;
                }
        }
        public int getPage() {				//显示的页码
                return page;
        }
        public void setPage(int newpage) {
                page = newpage;
        }
        public int getPageSize(){			//每页显示的图书数
                return pageSize;
        }
        public void setPageSize(int newpsize) {
                pageSize = newpsize;
        }
        public int getPageCount() {				//页面总数
                return pageCount;
        }
        public void setPageCount(int newpcount) {
                pageCount = newpcount;
        }
        public long getRecordCount() {
                return recordCount;
        }
        public void setRecordCount(long newrcount) {
                recordCount= newrcount;
        }
  public op_book() {
       }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -