package impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import vo.BookBorrow;
import dao.BookBorrowDao;
import dbc.DBConnection;
public class BookBorrowDaoImpl implements BookBorrowDao {
boolean flag=false;
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
DBConnection dbc=new DBConnection();
public List<BookBorrow> findbookborrow(String id) throws Exception {
List< BookBorrow> l=new ArrayList<BookBorrow>();
try {
conn=dbc.getConnection();
String sql="select readerinfo.readerid,bookinfo.bookid,readername,bookname," +
"borrowdate,returndate from readerinfo,bookinfo,borrowinfo where " +
" readerinfo.readerid=borrowinfo.readerid and bookinfo.bookid=borrowinfo.bookid " +
" and borrowinfo.returndate IS null and readerinfo.readerid='"+id+"'";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()){
BookBorrow book=new BookBorrow();
book.setBookname(rs.getString("bookname"));
book.setBookid(rs.getInt("bookid"));
book.setBdate(rs.getString("borrowdate"));
SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date bdate=smf.parse(book.getBdate());
Calendar c=Calendar.getInstance();
c.setTime(bdate);
c.add(c.DATE, 30);
Date srdate=c.getTime();
String srdatetime =smf.format(srdate);
book.setSrdate(srdatetime);
l.add(book);
}rs.close();
pstmt.close();
dbc.closed();}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return l;
}
public void borrowbook(BookBorrow b) throws Exception {
conn=dbc.getConnection();
String sql="insert into borrowinfo(bookid, readerid, borrowdate, returndate, fine) values(?,?,?,?,?) ";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,b.getBookid());
pstmt.setInt(2,b.getReaderid());
Date d=new Date();
SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String bdate=smf.format(d);
pstmt.setString(3, bdate);
pstmt.setString(4, null);
pstmt.setString(5, null);
pstmt.executeUpdate();
String upreader="update readerinfo set borrownumber=borrownumber+1 where readerid=?";
pstmt=conn.prepareStatement(upreader);
pstmt.setInt(1,b.getReaderid());
pstmt.executeUpdate();
String ubook="update bookinfo set nownumber=nownumber-1 where bookid=?";
pstmt=conn.prepareStatement(ubook);
pstmt.setInt(1,b.getBookid());
pstmt.executeUpdate();
pstmt.close();
dbc.closed();
}
public boolean checknumber(int id) throws Exception {
conn=dbc.getConnection();
String sql="select * from readerinfo where readerid=? ";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
while(rs.next()){
int a=rs.getInt("number");
if(a>0){
flag=true;}
}
return flag;
}
public void returnbook(BookBorrow b) throws Exception {
// TODO Auto-generated method stub
conn=dbc.getConnection();
String sql="update borrowinfo set returndate=?, renew=?,fine=? where bookid=? and readerid=? and borrowdate=?";
Date d=new Date();
SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String rdate=smf.format(d);
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,rdate);
pstmt.setString(2,b.getSrdate());
pstmt.setDouble(3, b.getFine());
pstmt.setInt(4, b.getBookid());
pstmt.setInt(5, b.getReaderid());
pstmt.setString(6,b.getBdate());
pstmt.executeUpdate();
String upreader="update readerinfo set borrownumber=borrownumber-1 where readerid=?";
pstmt=conn.prepareStatement(upreader);
pstmt.setInt(1,b.getReaderid());
pstmt.executeUpdate();
String ubook="update bookinfo set nownumber=nownumber+1 where bookid=?";
pstmt=conn.prepareStatement(ubook);
pstmt.setInt(1,b.getBookid());
pstmt.executeUpdate();
pstmt.close();
dbc.closed();
}
public List<BookBorrow> findbookborrowall() throws Exception {
// TODO Auto-generated method stub
List< BookBorrow> l=new ArrayList<BookBorrow>();
try {
conn=dbc.getConnection();
String sql="select readerinfo.readerid,bookinfo.bookid,readername,bookname," +
"borrowdate,returndate,fine from readerinfo,bookinfo,borrowinfo where " +
" readerinfo.readerid=borrowinfo.readerid and bookinfo.bookid=borrowinfo.bookid ";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()){
BookBorrow book=new BookBorrow();
book.setBookname(rs.getString("bookname"));
book.setBookid(rs.getInt("bookid"));
book.setBdate(rs.getString("borrowdate"));
book.setRdate(rs.getString("returndate"));
SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date bdate=smf.parse(book.getBdate());
Calendar c=Calendar.getInstance();
c.setTime(bdate);
c.add(c.DATE, 30);
Date srdate=c.getTime();
String srdatetime =smf.format(srdate);
book.setSrdate(srdatetime);
book.setReaderid(rs.getInt("readerid"));
book.setReadername(rs.getString("readername"));
book.setFine(rs.getDouble("fine"));
l.add(book);
}rs.close();
pstmt.close();
dbc.closed();}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return l;
}
public List<BookBorrow> findbookborrowareader(Integer id) throws Exception {
// TODO Auto-generated method stub
List< BookBorrow> l=new ArrayList<BookBorrow>();
try {
conn=dbc.getConnection();
String sql="select readerinfo.readerid,bookinfo.bookid,readername,bookname," +
"borrowdate,returndate,fine from readerinfo,bookinfo,borrowinfo where " +
" readerinfo.readerid=borrowinfo.readerid and bookinfo.bookid=borrowinfo.bookid and readerinfo.readerid='"+id+"' ";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()){
BookBorrow book=new BookBorrow();
book.setBookname(rs.getString("bookname"));
book.setBookid(rs.getInt("bookid"));
book.setBdate(rs.getString("borrowdate"));
book.setRdate(rs.getString("returndate"));
SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date bdate=smf.parse(book.getBdate());
Calendar c=Calendar.getInstance();
c.setTime(bdate);
c.add(c.DATE, 30);
Date srdate=c.getTime();
String srdatetime =smf.format(srdate);
book.setSrdate(srdatetime);
book.setReaderid(rs.getInt("readerid"));
book.setReadername(rs.getString("readername"));
book.setFine(rs.getDouble("fine"));
l.add(book);
}rs.close();
pstmt.close();
dbc.closed();}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return l;
}
public boolean checkuser(int id) throws Exception {
conn=dbc.getConnection();
String sql="select * from borrowinfo where readerid=? ";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
while(rs.next()){
flag=true;
}
return flag;
}
}