/*访问数据库,查询书的信息,处理购书事物
-books : ArrayList
-dbUrl : String
-dbUser : String
-dbPwd : String
+BookDB ();
+getConnection() : Connection
+closeConnection : void
+closePrepStmt(prepStmt : PreparedStatement) : void
+closeResultSet(rs : ResultSet) : void
+getNumberOfBooks() : int
+getBooks() : Collection
+getBookDetails(bookId : String) : BookDetails
+buyBooks(cart : ShoppingCart) : void
+buyBook(bookId : String, quantity : int, con : Connection) : void */
package bookstore;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import com.mysql.jdbc.Driver;
public class BookDB {
private ArrayList books;
private String dbUrl="jdbc:mysql://localhost:3306/BookDB";
private String dbUser="root";
private String dbPwd="root";
public BookDB (){
}
//获取与数据库的链接
public Connection getConnection(){
Connection conn=null;
try {
Class.forName(Driver.class.getName());
try {
conn=DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据链接
public void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//关闭查询语句
public void closePrepStmt(PreparedStatement prepStmt) {
if (prepStmt != null) {
try {
prepStmt.close();
prepStmt = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//关闭结果集
public void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//获得书目种类的数量
public int getNumberOfBooks(){
String selectStatement = "select count(*) from books";
int n = 0;
Connection conn=null;
try {
conn=getConnection();
Statement stmt = conn.createStatement();
n = stmt.executeUpdate(selectStatement);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
//得到购买书的总数
public Collection<BookDetails> getBooks() throws SQLException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<BookDetails> bookList = new ArrayList<BookDetails>();
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from books");
while (rs.next()) {
BookDetails book = new BookDetails(rs.getString(1), rs
.getString(2), rs.getString(3), rs.getFloat(4), rs
.getInt(5), rs.getString(6), rs.getInt(7));
bookList.add(book);
}
return bookList;
} finally {
closeResultSet(rs);
closeConnection(conn);
}
}
//得到书的详细情况
public BookDetails getBookDetails(String bookId) throws SQLException{
Connection conn=null;
// PreparedStatement prepStmt=null;
Statement stmt=null;
ResultSet rs=null;
try
{
conn = getConnection();
// prepStmt = conn.prepareStatement("select * from books where id = '?'");
// prepStmt.setString(1, bookId);
// rs=prepStmt.executeQuery();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from books where id = '"+bookId+"'");
BookDetails book=null;
//从数据库中获取书目详细信息,对构造方法赋值。将其初始化
if(rs.next())
{
book = new BookDetails(rs.getString(1), rs
.getString(2), rs.getString(3), rs.getFloat(4), rs
.getInt(5), rs.getString(6), rs.getInt(7));
}
return book;
}
finally
{
closeResultSet(rs);
closeConnection(conn);
}
}
//此方法定义是为了更新数据库使用。。并未用到
public void buyBooks(ShoppingCart cart) throws SQLException{
Connection conn = null;
PreparedStatement prepStmt = null;
Iterator<ShoppingCartItem> it = cart.getItems().iterator();
try {
conn = getConnection();
String sql = "update books set amount = amount - ? where id = ?";
prepStmt = conn.prepareStatement(sql);
while (it.hasNext()) {
ShoppingCartItem item = (ShoppingCartItem) it.next();
BookDetails book = (BookDetails) item.getItem();
String bookId = book.getBookId();
int quantity = item.getQuantity();
prepStmt.setInt(1, quantity);
prepStmt.setString(2, bookId);
prepStmt.addBatch();
}
prepStmt.executeBatch();
} finally {
closeConnection(conn);
}
}
//此方法为判断数据库书是否还有余量
public void buyBook(String bookId, int quantity, Connection con) throws SQLException{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
boolean bEnough=false;
try
{
conn=getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select amount from books where id = "+bookId);
while(rs.next())
{
int amount=rs.getInt(1);
if(amount >= quantity)
bEnough=true;
}
}
finally
{
closeResultSet(rs);
closeConnection(conn);
}
}
/**
* 通过关键字搜索图书的信息。
*/
public Collection<BookDetails> searchBook(String keyword)
throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<BookDetails> bookList = new ArrayList<BookDetails>();
try {
conn = getConnection();
stmt = conn.createStatement();
String sql = "select * from books where id like '%" + keyword
+ "%'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
BookDetails book = new BookDetails(rs.getString(1), rs
.getString(2), rs.getString(3), rs.getFloat(4), rs
.getInt(5), rs.getString(6), rs.getInt(7));
bookList.add(book);
}
return bookList;
} finally {
closeResultSet(rs);
closeConnection(conn);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
bookstore图书销售系统
共43个文件
jsp:10个
html:8个
class:5个
需积分: 9 28 下载量 155 浏览量
2008-09-17
22:14:52
上传
评论
收藏 35KB RAR 举报
温馨提示
此图书系统是用纯jsp和java编写,此图书系统是用纯jsp和java编写
资源推荐
资源详情
资源评论
收起资源包目录
bookstore.rar (43个子文件)
bookstore
.project 854B
bookstore
showcart.jsp 3KB
cashier.jsp 1005B
WEB-INF
web.xml 419B
lib
classes
bookstore
BookDetails.class 2KB
ShoppingCart.class 3KB
ShoppingCartItem.class 1KB
BookDB.class 6KB
inter
Comparable.class 151B
sql
BookDB.sql 2KB
html(演示版本)
bookstore.html 834B
cashier.html 807B
banner.html 699B
showcart_null.html 738B
catalog.html 844B
tag.bmp 26KB
receipt.html 461B
showcart.html 1KB
BookDetails.html 914B
META-INF
MANIFEST.MF 39B
catalog.jsp 2KB
bookstore.jsp 955B
common
logo.bmp 26KB
error.jsp 251B
banner.jsp 61B
common.jsp 145B
receipt.jsp 758B
search.jsp 2KB
bookDetails.jsp 2KB
.settings
.jsdtscope 454B
org.eclipse.wst.common.component 405B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.common.project.facet.core.xml 269B
org.eclipse.jst.common.project.facet.core.prefs 288B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
bookstore
BookDB.java 6KB
ShoppingCart.java 3KB
BookDetails.java 2KB
ShoppingCartItem.java 1KB
inter
Comparable.java 86B
sql
BookDB.sql 2KB
.classpath 560B
共 43 条
- 1
资源评论
NeoPwnR
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功