?? booksmn.java
字號:
package org.pan.web;
import java.sql.*;
import java.util.Vector;
import org.pan.util.*;
import javax.servlet.http.HttpServletRequest;
import org.pan.web.book.books;
/**
* Title: 圖書類別列表
* Description:
* Copyright: Copyright (c) 2003
* Company: 多智能網絡有限公司
* @author: siny pan
* @version 1.0
*/
public class booksmn extends DataBase {
private books abooks = new books(); //新的圖書類
private javax.servlet.http.HttpServletRequest request; //建立頁面請求
private boolean sqlflag = true ; //對接收到的數據是否正確
private Vector booklist; //顯示圖書列表向量數組
private int page = 1; //顯示的頁碼
private int pageSize=10; //每頁顯示的圖書數
private int pageCount =0; //頁面總數
private long recordCount =0; //查詢的記錄總數
public booksmn() throws Exception{
super();
}
public Vector getBooklist() {
return booklist;
}
public boolean getSqlflag() {
return sqlflag;
}
public String getGbk( String str) {
try
{
return new String(str.getBytes("ISO8859-1"));
}
catch (Exception e)
{
return str;
}
}
//將頁面表單傳來的資料分解
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(getGbk(bookname));
String author = request.getParameter("author");
if (author==null || author.equals(""))
{
author = "";
sqlflag = false;
}
abooks.setAuthor(getGbk(author));
String publish = request.getParameter("publish");;
if (publish==null)
{
publish = "";
}
abooks.setPublish(getGbk(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(getGbk(bookno));
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);
String content = request.getParameter("content");
if (content == null)
{
content = "";
}
abooks.setContent(getGbk(content));
if (sqlflag)
{
flag = true;
}
return flag;
}
catch (Exception e)
{
return flag;
}
}
public String getSql() {
sqlStr = "select id,classname from my_Book order by id";
return sqlStr;
}
public boolean execute(HttpServletRequest res) throws Exception {
request = res;
String PAGE = request.getParameter("page"); //頁碼
String classid = request.getParameter("classid"); //分類ID號
String keyword = request.getParameter("keyword"); //查詢關鍵詞
if (classid==null) classid="";
if (keyword==null) keyword = "";
keyword = getGbk(keyword).toUpperCase();
try
{
page = Integer.parseInt(PAGE);
}
catch (NumberFormatException e)
{
page = 1;
}
//取出記錄數
if (!classid.equals("") && keyword.equals("") ) {
sqlStr = "select count(*) from my_book where bookclass='" + classid + "'";
} else if (!keyword.equals("")) {
if (classid.equals(""))
{
sqlStr = "select count(*) from my_book where upper(bookname) like '%" +keyword+ "%' or upper(content) like '%" + keyword + "%'";
} else {
sqlStr = "select count(*) from my_book where bookclass='" + classid + "' and (upper(bookname) like '%" +keyword+ "%' or upper(content) like '%" + keyword + "%')";
}
} else {
sqlStr = "select count(*) from my_book";
}
int rscount = pageSize;
try
{
ResultSet rs1 = stmt.executeQuery(sqlStr);
if (rs1.next()) recordCount = rs1.getInt(1);
rs1.close();
}
catch (SQLException e)
{
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 " + pageSize + " 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 from My_book a,My_bookclass b where a.Bookclass = b.Id ";
if (!classid.equals("") && keyword.equals("") ){ //如果類別不為空,非查詢
if (page == 1)
{
sqlStr = sqlStr + " and a.bookclass='" + classid + "' order by a.Id desc";
} else {
sqlStr = sqlStr + " and a.bookclass='" + classid + "' and a.Id not in ( select TOP " + (recordCount-pageSize * page ) +" Id from My_book order by Id ) and a.Id in " +
"(select TOP " + (recordCount - pageSize * (page-1)) + " Id from My_book ORDER BY Id ) order by a.Id desc";
}
} else if (!keyword.equals("")) { //如果是查詢資料
if (page == 1)
{
if (!classid.equals("")) //查詢某一類
{
sqlStr = sqlStr + "and a.Bookclass='" + classid + "' and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') order by a.Id desc";
} else { //查詢所有類
sqlStr = sqlStr + " and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') order by a.Id desc";
}
} else {
if (!classid.equals(""))
{
sqlStr = sqlStr + " and a.Bookclass='" + classid + "' and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') and a.Id not in ( select TOP " + (recordCount-pageSize * page) + " Id from My_book ORDER BY Id ) and a.Id in " +
"(select TOP " + (recordCount - pageSize * (page-1)) + " Id from My_book ORDER BY Id ) " + " order by a.Id desc";
} else {
sqlStr = sqlStr + " and (upper(a.bookname) like '%" +keyword+ "%' or upper(a.content) like '%" + keyword + "%') and a.Id not in ( select TOP " + (recordCount-pageSize * page) + " Id from My_book ORDER BY Id ) and a.Id in " +
"(select TOP " + (recordCount - pageSize * (page-1)) + " Id from My_book ORDER BY Id ) " + " order by a.Id desc";
}
}
} else { //非查詢,也非分類瀏覽
if (page == 1)
{
sqlStr = sqlStr + " order by a.Id desc";
} else {
sqlStr = sqlStr + " and a.Id not in ( select TOP " + (recordCount-pageSize * page ) + " Id from My_book ORDER BY Id ) and a.Id in " +
"(select TOP " + (recordCount - pageSize * (page-1)) + " Id from My_book ORDER BY Id) order by a.Id desc";
}
}
try
{
rs = stmt.executeQuery(sqlStr);
booklist = new Vector(rscount);
while (rs.next())
{
books book = new books();
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"));
booklist.addElement(book);
}
rs.close();
return true;
}
catch (SQLException e)
{
System.out.println(e);
return false;
}
}
public boolean insert() throws Exception {
sqlStr = "insert into my_book (Bookname,Bookclass,Author,Publish,Bookno,Content,Prince,Amount,Leav_number,Regtime) values ('";
sqlStr = sqlStr + strFormat.toSql(abooks.getBookName()) + "','";
sqlStr = sqlStr + abooks.getBookClass() + "','";
sqlStr = sqlStr + strFormat.toSql(abooks.getAuthor()) + "','";
sqlStr = sqlStr + strFormat.toSql(abooks.getPublish()) + "','";
sqlStr = sqlStr + strFormat.toSql(abooks.getBookNo()) + "','";
sqlStr = sqlStr + strFormat.toSql(abooks.getContent()) + "','";
sqlStr = sqlStr + abooks.getPrince() + "','";
sqlStr = sqlStr + abooks.getAmount() + "','";
sqlStr = sqlStr + abooks.getAmount() + "',";
sqlStr = sqlStr + "getdate())";
try
{
stmt.execute(sqlStr);
return true;
}
catch (SQLException sqle)
{
return false;
}
}
public boolean update() throws Exception {
sqlStr = "update my_book set ";
sqlStr = sqlStr + "bookname = '" + strFormat.toSql(abooks.getBookName()) + "',";
sqlStr = sqlStr + "bookclass = '" + abooks.getBookClass() + "',";
sqlStr = sqlStr + "Author = '" + strFormat.toSql(abooks.getAuthor()) + "',";
sqlStr = sqlStr + "publish = '" + strFormat.toSql(abooks.getPublish()) + "',";
sqlStr = sqlStr + "bookno = '" + strFormat.toSql(abooks.getBookNo()) + "',";
sqlStr = sqlStr + "content = '" + strFormat.toSql(abooks.getContent()) + "',";
sqlStr = sqlStr + "prince = '" + abooks.getPrince() + "',";
sqlStr = sqlStr + "Amount = '" + abooks.getAmount() + "',";
sqlStr = sqlStr + "leav_number = '" + abooks.getAmount() + "' ";
sqlStr = sqlStr + "where id = '" + abooks.getId() + "'";
System.out.println(sqlStr);
try
{
stmt.execute(sqlStr);
return true;
}
catch (SQLException e)
{
return false;
}
}
public boolean delete( int aid ) throws Exception {
sqlStr = "delete from My_book where id = " + aid ;
try
{
stmt.execute(sqlStr);
return true;
}
catch (SQLException e)
{
System.out.println(e);
return false;
}
}
public boolean getOnebook(int newid ) throws Exception {
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 from My_book a,My_bookclass b where a.Bookclass=b.Id and a.Id = " + newid ;
rs = stmt.executeQuery(sqlStr);
if (rs.next())
{ booklist = new Vector(1);
books book = new books();
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"));
booklist.addElement(book);
} else {
rs.close();
return false;
}
rs.close();
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;
}
};
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -