package com.book.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.book.bean.Book;
import com.db.DBCon;
public class BookDAOImp implements BookDAO{
DBCon db=new DBCon();
Connection con=null;
Statement stmt=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public BookDAOImp() {
super();
}
public List<Book> seleceAll() {
//查询所有图书信息
List<Book> list=new ArrayList<Book>();
con=db.getConnection();
if(con!=null)
{
try {
stmt=con.createStatement();
String sql="select * from tb_book";
rs=stmt.executeQuery(sql);
while(rs.next())
{
int bid=rs.getInt(1);
String bookname=rs.getString(2);
String writer=rs.getString(3);
String publisher=rs.getString(4);
String pubdate=rs.getString(5);
double yprice=rs.getDouble(6);
double tprice=rs.getDouble(7);
String tupian=rs.getString(8);
String pingjia=rs.getString(9);
Book b=new Book();
b.setBid(bid);
b.setBookname(bookname);
b.setPingjia(pingjia);
b.setPubdate(pubdate);
b.setPublisher(publisher);
b.setTprice(tprice);
b.setTupian(tupian);
b.setWriter(writer);
b.setYprice(yprice);
list.add(b);
}
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public boolean addBook(Book book) {
// 添加图书
boolean result=false;
con=db.getConnection();
if(con!=null){
String sql="insert into tb_book(bookname,writer,publisher,pubdate,yprice,tprice,tupian,pingjia) values(?,?,?,?,?,?,?,?)";
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getBookname());
pstmt.setString(2, book.getWriter());
pstmt.setString(3, book.getPublisher());
pstmt.setString(4, book.getPubdate());
pstmt.setDouble(5, book.getYprice());
pstmt.setDouble(6, book.getTprice());
pstmt.setString(7, book.getTupian());
pstmt.setString(8, book.getPingjia());
int i=pstmt.executeUpdate();
if(i>0){
result =true;
}else{
result=false;
}
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public boolean removeBook(int id) {
// 根据序号删除图书信息
boolean result=false;
con=db.getConnection();
if(con!=null){
String sql="delete from tb_book where bid=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
int i=pstmt.executeUpdate();
if(i>0){
result =true;
}else{
result=false;
}
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public boolean updateBook(int id,Book book) {
// 根据编号修改图书信息
boolean result=false;
con=db.getConnection();
if(con!=null){
String sql="update tb_book set bookname=?,writer=?,publisher=?,pubdate=?,yprice=?,tprice=?,pingjia=? where bid=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getBookname());
pstmt.setString(2, book.getWriter());
pstmt.setString(3, book.getPublisher());
pstmt.setString(4, book.getPubdate());
pstmt.setDouble(5, book.getYprice());
pstmt.setDouble(6, book.getTprice());
pstmt.setString(7, book.getPingjia());
pstmt.setInt(8, id);
int i=pstmt.executeUpdate();
if(i>0){
result =true;
}else{
result=false;
}
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public Book getBookbyId(int id) {
//根据编号查找图书信息
Book b=new Book();
con=db.getConnection();
if(con!=null){
String sql="select * from tb_book where bid=?";
try {
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
b.setBid(rs.getInt(1));
b.setBookname(rs.getString(2));
b.setWriter(rs.getString(3));
b.setPublisher(rs.getString(4));
b.setPubdate(rs.getString(5));
b.setYprice(rs.getDouble(6));
b.setTprice(rs.getDouble(7));
b.setTupian(rs.getString(8));
b.setPingjia(rs.getString(9));
}
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(con!=null) con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return b;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
图书购物车(session+JAVABean+Servlet)
共59个文件
jpg:21个
java:7个
class:7个
需积分: 28 10 下载量 105 浏览量
2018-01-02
10:16:57
上传
评论
收藏 1.57MB ZIP 举报
温馨提示
图书购物车(session+JAVABean+Servlet);很简单的一个图书购物车
资源推荐
资源详情
资源评论
收起资源包目录
图书购物车(session+JAVABean+Servlet).zip (59个子文件)
图书购物车(session+JAVABean+Servlet)
shoppingcart
.project 2KB
db
db_bookshop_Log.LDF 1024KB
db_bookshop_Data.MDF 1.56MB
db_bookshop.sql 6KB
.mymetadata 309B
src
com
db
DBCon.java 589B
cart
bean
Cart.java 3KB
control
CartServlet.java 3KB
book
dao
BookDAOImp.java 5KB
BookDAO.java 301B
bean
Book.java 2KB
my
util
EncodingChange.java 463B
META-INF
MANIFEST.MF 25B
WebRoot
WEB-INF
classes
com
db
DBCon.class 971B
cart
bean
Cart.class 3KB
control
CartServlet.class 3KB
book
dao
BookDAO.class 380B
BookDAOImp.class 5KB
bean
Book.class 3KB
my
util
EncodingChange.class 744B
META-INF
MANIFEST.MF 25B
lib
mysql-connector-java-5.1.18-bin.jar 771KB
web.xml 822B
index.jsp 829B
images
bg.jpg 215KB
19.jpg 10KB
2.jpg 13KB
11.jpg 9KB
13.jpg 14KB
8.jpg 13KB
6.jpg 14KB
top.png 80KB
1.jpg 12KB
16.jpg 12KB
12.jpg 12KB
14.jpg 10KB
4.jpg 10KB
gwc.png 2KB
5.jpg 12KB
goumai.jpg 14KB
17.jpg 12KB
9.jpg 13KB
18.jpg 8KB
10.jpg 9KB
7.jpg 10KB
15.jpg 6KB
3.jpg 13KB
cart
item.jsp 524B
cartmain.jsp 2KB
showbook.jsp 3KB
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 412B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.migration.prefs 382B
org.eclipse.wst.common.component 562B
.jsdtscope 500B
.classpath 1KB
共 59 条
- 1
资源评论
fan10001
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功