public void modifyBook(long bookid,int stock)throws Exception { Statement stmt =conn.createStatement(); int rs=stmt.executeUpdate("update bookinfo set stock='"+stock+"'where id='"+bookid+"'"); conn.close(); }
public void uploadBook(String isbn,String p)throws Exception { Statement stmt =conn.createStatement(); int rs=stmt.executeUpdate("update bookinfo set picture='"+p+"'where isbn='"+isbn+"'"); conn.close(); }
public void deleteBook(long bookId)throws Exception { Statement stmt=conn.createStatement(); int rs=stmt.executeUpdate("delete from bookinfo where id='"+bookId+"'"); conn.close(); }
public BookInfo getBookInfo(long bookId)throws Exception { Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select * from bookinfo where id='"+bookId+"'"); BookInfo book=null; while(rs.next()) { book =new BookInfo(); book.setId(rs.getLong("id")); book.setBookName(rs.getString("bookname")); book.setBookClass(rs.getInt("bookclass")); book.setAuthor(rs.getString("author")); book.setPublisher(rs.getString("publisher")); book.setIsbn(rs.getString("isbn")); book.setContent(rs.getString("content")); book.setPrice(rs.getFloat("price")); book.setStock(rs.getInt("stock")); book.setRegTime(rs.getDate("regtime").toString()); book.setPicture(rs.getString("picture")); } //conn.close(); return book; }
public boolean isEnough(int amount,long bookId) { try {
Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery("select stock from bookinfo where id='"+bookId+"'");
//检查查看的页面数是否在范围内 if (page < 1) page = 1; else if (page > pageCount) page = pageCount;
sqlStr="select * from bookinfo";
if (page == 1) { sqlStr = sqlStr + " order by id limit 0,"+pageSize; } else { sqlStr = sqlStr + " order by id limit "+(pageSize*(page-1))+","+pageSize;
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %><%@ page import="ch10.*" %>
<jsp:useBean id="bookDB" scope="page" class="ch10.BookDB" /><%
request.setCharacterEncoding("GB2312");
%> <%
int totalPage=0;
long totalRecord=0;
int pageSize=10;
int pagen=1;
Vector bookList=new Vector();
if(request.getParameter("pagen")!=null)
{
pagen=Integer.parseInt(request.getParameter("pagen"));
} %>
<html>
<head>
<title>网上书店 选购图书</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">function openScript(url,name, width, height){
var Win = window.open(url,name,'width=' + width + ',height=' + height + ',resizable=1,scrollbars=yes,menubar=no,status=yes' );
}</script>
<link rel="stylesheet" href="books.css" type="text/css">
</head><body bgcolor="#FFFDFD" text="#000000">
<div align="center">
<table width="750" border="0" cellspacing="1" cellpadding="1">
<tr bgcolor="#A4A4A4">
<td width="120"> </td>
<td width="80" align="center"><a href="./index.jsp"><FONT SIZE="4">首页</FONT></a></td>
<td width="80" align="center"><a href="booklist.jsp"><FONT SIZE="4">在线购物</FONT></a></td>
<td width="100" align="center"><a href="shoppingcar.jsp"><FONT SIZE="4">我的购物车</FONT></a></td>
<td width="80" align="center"><a href="login.jsp"><FONT SIZE="4">用户登录</FONT></a></td>
<td width="80" align="center"><a href="userinfo.jsp"><FONT SIZE="4">用户信息</FONT></a></td>
<td width="80" align="center"><a href="logout.jsp"><FONT SIZE="4">离开网站</FONT></a></td>
<td width="80" align="center"><a href="manage/login.jsp"><FONT SIZE="4">网站管理</FONT></a></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td align="center" width="23%" valign="top">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td> </td>
</tr>
<tr>
<td align="left"><a href="userinfo.jsp"><FONT SIZE="4">用户信息</FONT></a></td>
</tr>
<tr>
<td align="left"><a href="shoppingcar.jsp"><FONT SIZE="4">我的购物车</FONT></a></td>
</tr>
<tr>
<td align="left"><a href="logout.jsp"></FONT>离开网站</FONT></a></td>
</tr>
</table>
</td>
<td align="center" width="77%">
<p> </p>
<p>网上书店所有图书情况:</p>
<table width="100%" border="1" cellspacing="1" cellpadding="1" bordercolor="#CC9966">
<tr align="left">
<td>图书名称</td>
<td>作者</td>
<td>出版社</td>
<td>单价</td>
<td width=110>购买</td>
<td>详细信息</td>
</tr><%
String pagem=request.getParameter("pagen");
if(bookDB.execute(pagem))
{
totalRecord=bookDB.getRecordCount();
totalPage=(int)totalRecord/pageSize+1;
bookList = bookDB.getBookList();
Enumeration e= bookList.elements();
while(e.hasMoreElements())
{
BookInfo tempBook = (BookInfo)e.nextElement();
%>
<tr align="left">
<td><%=tempBook.getBookName()%></td>
<td><%=tempBook.getAuthor()%></td>
<td><%=tempBook.getPublisher()%></td>
<td><%=tempBook.getPrice()%></td>
<td align="center"><a href="#" onClick="openScript('purchase.jsp?bookid=<%=tempBook.getId()%>','pur',400,450)">购买</a> </td>
<td align="center"><a href="#" onClick="openScript('showbook.jsp?bookid=<%=tempBook.getId()%>','show',400,450)">详细信息</a> </td>
</tr>
<%
}
}
else
out.print("连接数据库出错!");
%> </table>
<form name="form1" action="booklist.jsp" method="post">
<table width="90%" border="0" cellspacing="1" cellpadding="1"> <tr>
<td align="left">总计结果为<%= totalRecord %>条,共<%=totalPage%>页 当前页第<%=pagen%>页
<a href="booklist.jsp">首页</a>
<% if (pagen>1)
{ %>
<a href="booklist.jsp?page=<%=pagen-1 %>">上一页</a>
<% } %>
<% if (pagen<=totalPage-1)
{%>
<a href="booklist.jsp?pagen=<%=pagen+1 %>">下一页</a>
<% } %>
<a href="booklist.jsp?pagen=<%=totalPage %>">未页</a>
</td>
<td>
<p>跳转到:<input type="text" name="pagen" size="6" value="<%=pagen%>"> <INPUT TYPE="submit" name="submit" value="go">
</p>
</td>
</tr>
</table>
</form>
<br>
</div>
</body>
</html>
import java.sql.*;
import java.util.*;public class BookDB
{
private Connection conn;
private Vector bookList;
private int page = 1; //显示的页码
private int pageSize=10; //每页显示的图书数
private int pageCount =0; //页面总数
private long recordCount =0;
public BookDB()
{
try
{
conn=DataBaseConnection.getConnection();
bookList = new Vector();
}
catch(Exception e)
{
e.printStackTrace();
}
} public void setBookList()throws Exception
{
Statement stmt =conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookinfo");
while(rs.next())
{
BookInfo tempBook=new BookInfo();
tempBook.setId(rs.getLong("id"));
tempBook.setBookName(rs.getString("bookname"));
tempBook.setAuthor(rs.getString("author"));
tempBook.setBookClass(rs.getInt("bookclass"));
tempBook.setIsbn(rs.getString("isbn"));
tempBook.setPrice(rs.getFloat("price"));
tempBook.setPublisher(rs.getString("publisher"));
tempBook.setRegTime(rs.getDate("regtime").toString());
tempBook.setStock(rs.getInt("stock"));
tempBook.setContent(rs.getString("content"));
bookList.add(tempBook);
}
conn.close();
}
public void addBook(BookInfo book)throws Exception
{
PreparedStatement pstmt =conn.prepareStatement("insert into bookinfo(bookname,bookclass,author,publisher,isbn,content,price,stock,regtime)values(?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,book.getBookName());
pstmt.setInt(2,book.getBookClass());
pstmt.setString(3,book.getAuthor());
pstmt.setString(4,book.getPublisher());
pstmt.setString(5,book.getIsbn());
pstmt.setString(6,book.getContent());
pstmt.setFloat(7,book.getPrice());
pstmt.setInt(8,book.getStock());
pstmt.setDate(9,new java.sql.Date(new java.util.Date().getTime()));
pstmt.execute();
conn.close();
}
public void modifyBook(long bookid,int stock)throws Exception
{
Statement stmt =conn.createStatement();
int rs=stmt.executeUpdate("update bookinfo set stock='"+stock+"'where id='"+bookid+"'");
conn.close();
}
public void uploadBook(String isbn,String p)throws Exception
{
Statement stmt =conn.createStatement();
int rs=stmt.executeUpdate("update bookinfo set picture='"+p+"'where isbn='"+isbn+"'");
conn.close();
}
public void deleteBook(long bookId)throws Exception
{
Statement stmt=conn.createStatement();
int rs=stmt.executeUpdate("delete from bookinfo where id='"+bookId+"'");
conn.close();
}
public BookInfo getBookInfo(long bookId)throws Exception
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from bookinfo where id='"+bookId+"'");
BookInfo book=null;
while(rs.next())
{
book =new BookInfo();
book.setId(rs.getLong("id"));
book.setBookName(rs.getString("bookname"));
book.setBookClass(rs.getInt("bookclass"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setIsbn(rs.getString("isbn"));
book.setContent(rs.getString("content"));
book.setPrice(rs.getFloat("price"));
book.setStock(rs.getInt("stock"));
book.setRegTime(rs.getDate("regtime").toString());
book.setPicture(rs.getString("picture"));
}
//conn.close();
return book;
}
public boolean isEnough(int amount,long bookId)
{
try
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select stock from bookinfo where id='"+bookId+"'");
if(rs.next())
{
if(amount<=rs.getInt(1))
return true;
else
return false;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
}
/**
* 分页显示图书信息
*/
public boolean execute(String requestPage) throws Exception
{
String sqlStr=null;
try
{
page = Integer.parseInt(requestPage);
}
catch (NumberFormatException e)
{
page = 1;
}
//取出记录数
sqlStr = "select count(*) from bookinfo";
try
{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sqlStr);
if (rs.next())
recordCount = rs.getInt(1);
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
//设定总页面数量
if (recordCount < 1)
pageCount = 0;
else
pageCount = (int)(recordCount - 1) / pageSize + 1;
//检查查看的页面数是否在范围内
if (page < 1)
page = 1;
else if (page > pageCount)
page = pageCount;
sqlStr="select * from bookinfo";
if (page == 1)
{
sqlStr = sqlStr + " order by id limit 0,"+pageSize;
}
else
{
sqlStr = sqlStr + " order by id limit "+(pageSize*(page-1))+","+pageSize;
}
try
{
Statement stmt=conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlStr);
while (rs.next())
{
BookInfo book = new BookInfo();
book.setId(rs.getLong("id"));
book.setBookName(rs.getString("bookname"));
book.setBookClass(rs.getInt("bookclass"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setIsbn(rs.getString("isbn"));
book.setContent(rs.getString("content"));
book.setPrice(rs.getFloat("price"));
book.setStock(rs.getInt("stock"));
book.setRegTime(rs.getDate("regtime").toString());
bookList.add(book);
}
rs.close();
return true;
}
catch (SQLException e)
{
return false;
}
}
public Vector getBookList() throws Exception
{
return bookList;
}
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 newPageCount)
{
pageCount =newPageCount;
} public long getRecordCount()
{
return recordCount;
}
public void setRecordCount(long newrcount)
{
recordCount= newrcount;
}
}