package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.dao.BaseDAO;
import com.entity.Book;
import com.entity.BookOder;
public class BookDAO {
public boolean updateReturnBook(int bno){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = BaseDAO.getConnection();
String sql = "update book1 set bcount=bcount+1 where bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bno);
int rows = pstmt.executeUpdate();
if(rows > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
public boolean returnBook(int uno,int bno){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = BaseDAO.getConnection();
String sql = "delete from showBookBorrow1 where uno=? and bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, uno);
pstmt.setInt(2, bno);
int rows = pstmt.executeUpdate();
if(rows > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
public boolean updateRBook(int bno) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = BaseDAO.getConnection();
String sql = "update book1 set num=num+1 where bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bno);
int rows = pstmt.executeUpdate();
if(rows > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
public boolean updateBook(int bno) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = BaseDAO.getConnection();
String sql = "update book1 set bcount=bcount-1,num=num+1 where bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bno);
int rows = pstmt.executeUpdate();
if(rows > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
public boolean insertBorrow(int uno,String uname,int bno,String bname,String author,String publish){
Connection con = null;
PreparedStatement pstmt = null;
try {
con = BaseDAO.getConnection();
String sql = "insert into showbookborrow1 values(?,?,?,?,?,?,sysdate,add_months(sysdate,1),0)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, uno);
pstmt.setString(2, uname);
pstmt.setInt(3, bno);
pstmt.setString(4, bname);
pstmt.setString(5, author);
pstmt.setString(6, publish);
int rows = pstmt.executeUpdate();
if(rows > 0) {
return true;
} else {
return false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
/**
* 查询小说
* @return
*/
public ArrayList selectXiaoshuo(String xiaoshuo) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = BaseDAO.getConnection();
String sql = "select * from book1 where bclassify=? and bname like'%"+xiaoshuo+"%'";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "小说");
rs = pstmt.executeQuery();
if(rs != null) {
ArrayList list = new ArrayList();
while(rs.next()) {
Book book = new Book();
//满足条件就与type类中获得的值进行比较
//type.setTypeid(rs.getInt("typeid"));
//type.setTypename(rs.getString("typename"));
book.setBno(rs.getInt("bno"));
book.setBname(rs.getString("bname"));
book.setAuthor(rs.getString("author"));
book.setPublish(rs.getString("publish"));
book.setBcount(rs.getInt("bcount"));
book.setBclassify(rs.getString("bclassify"));
book.setBookrack(rs.getString("bookrack"));
list.add(book);
}
return list;
} else {
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
BaseDAO.closeResultSet(rs);
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
/**
* 查询文学
* @return
*/
public ArrayList selectWenxue(String wenxue) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = BaseDAO.getConnection();
String sql = "select * from book1 where bclassify=? and bname like'%"+wenxue+"%'";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "文学");
rs = pstmt.executeQuery();
if(rs != null) {
ArrayList list = new ArrayList();
while(rs.next()) {
Book book = new Book();
//满足条件就与type类中获得的值进行比较
//type.setTypeid(rs.getInt("typeid"));
//type.setTypename(rs.getString("typename"));
book.setBno(rs.getInt("bno"));
book.setBname(rs.getString("bname"));
book.setAuthor(rs.getString("author"));
book.setPublish(rs.getString("publish"));
book.setBcount(rs.getInt("bcount"));
book.setBclassify(rs.getString("bclassify"));
book.setBookrack(rs.getString("bookrack"));
list.add(book);
}
return list;
} else {
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
BaseDAO.closeResultSet(rs);
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
/**
* 查询传记
* @return
*/
public ArrayList selectZhuanji(String zhuanji) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = BaseDAO.getConnection();
String sql = "select * from book1 where bclassify=? and bname like'%"+zhuanji+"%'";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "传记");
rs = pstmt.executeQuery();
if(rs != null) {
ArrayList list = new ArrayList();
while(rs.next()) {
Book book = new Book();
//满足条件就与type类中获得的值进行比较
//type.setTypeid(rs.getInt("typeid"));
//type.setTypename(rs.getString("typename"));
book.setBno(rs.getInt("bno"));
book.setBname(rs.getString("bname"));
book.setAuthor(rs.getString("author"));
book.setPublish(rs.getString("publish"));
book.setBcount(rs.getInt("bcount"));
book.setBclassify(rs.getString("bclassify"));
book.setBookrack(rs.getString("bookrack"));
list.add(book);
}
return list;
} else {
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
BaseDAO.closeResultSet(rs);
BaseDAO.closeStatement(pstmt);
BaseDAO.closeConnection(con);
}
}
/**
* 查询艺术
* @return
*/
public ArrayList selectYishu(String yishu) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = BaseDAO.getConnection();
String sql = "select * from book1 where bclassify=? and bname like'%"+yishu+"%'";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "艺术");
rs = pstmt.executeQuery();
if(rs != null) {
ArrayList list = new ArrayList();
while(rs.next()) {
Book book = new Book();
//