BorrowDAO.java
上传用户:ycdfsj258
上传日期:2022-08-04
资源大小:1094k
文件大小:11k
源码类别:

图形图象

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.util.*;
  4. import com.actionForm.BorrowForm;
  5. import java.sql.*;
  6. import com.actionForm.ReaderForm;
  7. import com.actionForm.BookForm;
  8. import java.util.Date;
  9. public class BorrowDAO {
  10.     ConnDB conn = new ConnDB();
  11.     public int insert() {
  12.         String sql = "INSERT INTO tb_borrow (bookid) vlaues(1) ";
  13.         int ret = conn.executeUpdate(sql);
  14.         return ret;
  15.     }
  16.     //*****************************图书借阅******************************
  17.     public int insertBorrow(ReaderForm readerForm,BookForm bookForm,String operator){
  18.         //获取系统日期
  19.         Date dateU=new Date();
  20.         java.sql.Date date=new java.sql.Date(dateU.getTime());
  21.        String sql1="select t.days from tb_bookinfo b left join tb_booktype t on b.typeid=t.id where b.id="+bookForm.getId()+"";
  22.         ResultSet rs=conn.executeQuery(sql1);
  23.         int days=0;
  24.         try {
  25.             if (rs.next()) {
  26.                 days = rs.getInt(1);
  27.             }
  28.         } catch (SQLException ex) {
  29.         }
  30.         //计算归还时间
  31.           String date_str=String.valueOf(date);
  32.           String dd = date_str.substring(8,10);
  33.           String DD = date_str.substring(0,8)+String.valueOf(Integer.parseInt(dd) + days);
  34.           java.sql.Date backTime= java.sql.Date.valueOf(DD);
  35.           String sql ="Insert into tb_borrow (readerid,bookid,borrowTime,backTime,operator) values("+readerForm.getId()+","+bookForm.getId()+",'"+date+"','"+backTime+"','"+operator+"')";
  36.            int falg = conn.executeUpdate(sql);
  37.           System.out.println("添加图书借阅信息的SQL:" + sql);
  38.           conn.close();
  39.           return falg;
  40. }
  41.       //*************************************图书继借*********************************
  42.       public int renew(int id){
  43.           String sql0="SELECT bookid FROM tb_borrow WHERE id="+id+"";
  44.           ResultSet rs1=conn.executeQuery(sql0);
  45.           int flag=0;
  46.         try {
  47.             if (rs1.next()) {
  48.                 //获取系统日期
  49.                 Date dateU = new Date();
  50.                 java.sql.Date date = new java.sql.Date(dateU.getTime());
  51.                 String sql1 = "select t.days from tb_bookinfo b left join tb_booktype t on b.typeid=t.id where b.id=" +
  52.                               rs1.getInt(1) + "";
  53.                 ResultSet rs = conn.executeQuery(sql1);
  54.                 int days = 0;
  55.                 try {
  56.                     if (rs.next()) {
  57.                         days = rs.getInt(1);
  58.                     }
  59.                 } catch (SQLException ex) {
  60.                 }
  61.                 //计算归还时间
  62.                 String date_str = String.valueOf(date);
  63.                 String dd = date_str.substring(8, 10);
  64.                 String DD = date_str.substring(0, 8) +
  65.                             String.valueOf(Integer.parseInt(dd) + days);
  66.                 java.sql.Date backTime = java.sql.Date.valueOf(DD);
  67.                 String sql = "UPDATE tb_borrow SET backtime='" + backTime +
  68.                              "' where id=" + id + "";
  69.                 flag = conn.executeUpdate(sql);
  70.             }
  71.         } catch (NumberFormatException ex1) {
  72.         } catch (SQLException ex1) {
  73.         }
  74.           conn.close();
  75.           return flag;
  76.       }
  77.       //*************************************图书归还*********************************
  78.       public int back(int id,String operator){
  79.           String sql0="SELECT readerid,bookid FROM tb_borrow WHERE id="+id+"";
  80.           ResultSet rs1=conn.executeQuery(sql0);
  81.           int flag=0;
  82.         try {
  83.             if (rs1.next()) {
  84.                 //获取系统日期
  85.                 Date dateU = new Date();
  86.                 java.sql.Date date = new java.sql.Date(dateU.getTime());
  87.                 int readerid=rs1.getInt(1);
  88.                 int bookid=rs1.getInt(2);
  89.                 String sql1="INSERT INTO tb_giveback (readerid,bookid,backTime,operator) VALUES("+readerid+","+bookid+",'"+date+"','"+operator+"')";
  90.                 int ret=conn.executeUpdate(sql1);
  91.                 if(ret==1){
  92.                     String sql2 = "UPDATE tb_borrow SET ifback=1 where id=" + id +
  93.                                  "";
  94.                     flag = conn.executeUpdate(sql2);
  95.                 }else{
  96.                     flag=0;
  97.                 }
  98.             }
  99.         } catch (NumberFormatException ex1) {
  100.         } catch (SQLException ex1) {
  101.         }
  102.           conn.close();
  103.           return flag;
  104.       }
  105.     //*****************************查询图书借阅信息************************
  106.       public Collection borrowinfo(String str){
  107.       String sql="select borr.*,book.bookname,book.price,pub.pubname,bs.name bookcasename,r.barcode from (select * from tb_borrow where ifback=0) as borr left join tb_bookinfo book on borr.bookid=book.id join tb_publishing pub on book.isbn=pub.isbn join tb_bookcase bs on book.bookcase=bs.id join tb_reader r on borr.readerid=r.id where r.barcode='"+str+"'";
  108.       ResultSet rs=conn.executeQuery(sql);
  109.       Collection coll=new ArrayList();
  110.       BorrowForm form=null;
  111.       try {
  112.           while (rs.next()) {
  113.               form = new BorrowForm();
  114.               form.setId(Integer.valueOf(rs.getInt(1)));
  115.               form.setBorrowTime(rs.getString(4));
  116.               form.setBackTime(rs.getString(5));
  117.               form.setBookName(rs.getString(8));
  118.               form.setPrice(Float.valueOf(rs.getFloat(9)));
  119.               form.setPubName(rs.getString(10));
  120.               form.setBookcaseName(rs.getString(11));
  121.               coll.add(form);
  122.           }
  123.       } catch (SQLException ex) {
  124.           System.out.println("借阅信息:"+ex.getMessage());
  125.       }
  126.       conn.close();
  127.       return coll;
  128.       }
  129.       //*************************到期提醒******************************************
  130.     public Collection bremind(){
  131.     Date dateU = new Date();
  132.     java.sql.Date date = new java.sql.Date(dateU.getTime());
  133.     String sql="select borr.borrowTime,borr.backTime,book.barcode,book.bookname,r.name readername,r.barcode readerbarcode from tb_borrow borr join tb_bookinfo book on book.id=borr.bookid join tb_reader r on r.id=borr.readerid where borr.backTime <='"+date+"'";
  134.     ResultSet rs=conn.executeQuery(sql);
  135.     System.out.println("到时提醒的SQL:"+sql);
  136.     Collection coll=new ArrayList();
  137.     BorrowForm form=null;
  138.     try {
  139.         while (rs.next()) {
  140.             form = new BorrowForm();
  141.             form.setBorrowTime(rs.getString(1));
  142.             form.setBackTime(rs.getString(2));
  143.             form.setBookBarcode(rs.getString(3));
  144.             form.setBookName(rs.getString(4));
  145.             form.setReaderName(rs.getString(5));
  146.             form.setReaderBarcode(rs.getString(6));
  147.             coll.add(form);
  148.             System.out.println("图书条形码:"+rs.getString(3));
  149.         }
  150.     } catch (SQLException ex) {
  151.         System.out.println(ex.getMessage());
  152.     }
  153.     conn.close();
  154.     return coll;
  155. }
  156. //*************************图书借阅查询******************************************
  157. public Collection borrowQuery(String strif){
  158.     String sql="";
  159.     if(strif!="all" && strif!=null && strif!=""){
  160.         sql="select * from (select borr.borrowTime,borr.backTime,book.barcode,book.bookname,r.name readername,r.barcode readerbarcode,borr.ifback from tb_borrow borr join tb_bookinfo book on book.id=borr.bookid join tb_reader r on r.id=borr.readerid) as borr where borr."+strif+"";
  161.     }else{
  162.         sql="select * from (select borr.borrowTime,borr.backTime,book.barcode,book.bookname,r.name readername,r.barcode readerbarcode,borr.ifback from tb_borrow borr join tb_bookinfo book on book.id=borr.bookid join tb_reader r on r.id=borr.readerid) as borr";
  163. }
  164. ResultSet rs=conn.executeQuery(sql);
  165. System.out.println("图书借阅查询的SQL:"+sql);
  166. Collection coll=new ArrayList();
  167. BorrowForm form=null;
  168. try {
  169.     while (rs.next()) {
  170.         form = new BorrowForm();
  171.         form.setBorrowTime(rs.getString(1));
  172.         form.setBackTime(rs.getString(2));
  173.         form.setBookBarcode(rs.getString(3));
  174.         form.setBookName(rs.getString(4));
  175.         form.setReaderName(rs.getString(5));
  176.         form.setReaderBarcode(rs.getString(6));
  177.         form.setIfBack(rs.getInt(7));
  178.         coll.add(form);
  179.     }
  180. } catch (SQLException ex) {
  181.     System.out.println(ex.getMessage());
  182. }
  183. conn.close();
  184. return coll;
  185.     }
  186.       //*************************图书借阅排行******************************************
  187.     public Collection bookBorrowSort() {
  188.        String sql = "select * from (SELECT bookid,count(bookid) as degree FROM tb_borrow group by bookid) as borr join (select b.*,c.name as bookcaseName,p.pubname,t.typename from tb_bookinfo b left join tb_bookcase c on b.bookcase=c.id join tb_publishing p on b.ISBN=p.ISBN join tb_booktype t on b.typeid=t.id where b.del=0) as book on borr.bookid=book.id order by borr.degree desc limit 10 ";
  189.         System.out.println("图书借阅排行:"+sql);
  190.         Collection coll = new ArrayList();
  191.         BorrowForm form = null;
  192.         ResultSet rs = conn.executeQuery(sql);
  193.         try {
  194.             while (rs.next()) {
  195.                 form = new BorrowForm();
  196.                 form.setBookId(rs.getInt(1));
  197.                 form.setDegree(rs.getInt(2));
  198.                 form.setBookBarcode(rs.getString(3));
  199.                 form.setBookName(rs.getString(4));
  200.                 form.setAuthor(rs.getString(6));
  201.                 form.setPrice(Float.valueOf(rs.getString(9)));
  202.                 form.setStorage(rs.getInt(12));
  203.                 form.setBookcaseName(rs.getString(17));
  204.                 form.setPubName(rs.getString(18));
  205.                 form.setBookType(rs.getString(19));
  206.                 coll.add(form);
  207.                 System.out.print("RS:"+rs.getString(4));
  208.             }
  209.         } catch (SQLException ex) {
  210.             System.out.println(ex.getMessage());
  211.         }
  212.         conn.close();
  213.         return coll;
  214.     }
  215.     //***********************读者借阅排行*************************************
  216.     public Collection readerBorrowSort() {
  217.         String sql = "select * from (SELECT readerid,count(readerid) as degree FROM tb_borrow group by readerid) as borr join (select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id) as reader on borr.readerid=reader.id order by borr.degree desc limit 10";
  218.         Collection coll = new ArrayList();
  219.         BorrowForm form = null;
  220.         ResultSet rs = conn.executeQuery(sql);
  221.         try {
  222.             while (rs.next()) {
  223.                 form = new BorrowForm();
  224.                 form.setReaderId(rs.getInt(1));
  225.                 form.setDegree(rs.getInt(2));
  226.                 form.setReaderName(rs.getString(4));
  227.                 form.setSex(rs.getString(5));
  228.                 form.setReaderBarcode(rs.getString(6));
  229.                 form.setBirthday(rs.getString(8));
  230.                 form.setPaperType(rs.getString(9));
  231.                 form.setPaperNo(rs.getString(10));
  232.                 form.setTel(rs.getString(11));
  233.                 form.setReaderType(rs.getString(17));
  234.                 coll.add(form);
  235.             }
  236.         } catch (SQLException ex) {
  237.             System.out.println(ex.getMessage());
  238.         }
  239.         conn.close();
  240.         return coll;
  241.     }
  242. }