zoukankan      html  css  js  c++  java
  • 图书管理系统的dao层

    dao层

    package bean;
    
    import java.sql.*;
    import java.text.SimpleDateFormat;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.*;
    import java.util.Date;
    
    
    public class dao {
          
        public static Long between_days(String a, String b) {
    
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// �Զ���ʱ���ʽ
    
            Calendar calendar_a = Calendar.getInstance();// ��ȡ��������
            Calendar calendar_b = Calendar.getInstance();
    
            Date date_a = null;
            Date date_b = null;
    
            try {
                date_a = simpleDateFormat.parse(a);//�ַ���תDate
                date_b = simpleDateFormat.parse(b);
                calendar_a.setTime(date_a);// ��������
                calendar_b.setTime(date_b);
            } catch (Exception e) {
                e.printStackTrace();//��ʽ���쳣
            }
    
            long time_a = calendar_a.getTimeInMillis();
            long time_b = calendar_b.getTimeInMillis();
    
            long between_days = (time_b - time_a) / (1000 * 3600 * 24);//�����������
    
            return between_days;
        }
        
          public String passing(String account,String password) throws SQLException {          //login
              bd database=new bd();
              Connection connect=database.getlink();
              Statement state=null;
              ResultSet rsp=null;
              state=connect.createStatement();
              rsp=state.executeQuery("select * from name where code='"+account+"'and password='"+password+"'");
              if(rsp.next())
              {   System.out.print(rsp.getString("using"));
                  return rsp.getString("using");}
                return "0";
          }
          public readerform selfcheck(String code) throws SQLException {
              readerform me=new readerform();
              bd database=new bd();
                 Connection connect=database.getlink();
                 Statement state=null;
              state=connect.createStatement();
              ResultSet rs = state.executeQuery("select * from reader where code='"+code+"'");
              if(rs.next()) {
              me.setCode(rs.getString("code"));
              me.setName(rs.getString("name"));    
              me.setSex(rs.getString("sex"));   
              me.setAcd(rs.getString("acd"));
              }
              return me;  
          }
          
          public int Iputreader(readerform w){
              bd database=new bd();
              Connection connect=database.getlink();
              Statement state=null;
              try {
                state=connect.createStatement();
               } catch (SQLException e) {System.out.print("sqlboom!
    ");}
              try {
                state.executeUpdate("INSERT INTO reader VALUES('"+w.getCode()+"','"+w.getName()+"','"+w.getSex()+"','"+w.getAcd()+"')");
                state.executeUpdate("INSERT INTO name VALUES('"+w.getCode()+"','0000','2')");
                return 1;
               } catch (SQLException e) {System.out.print("insert error!
    ");return 0;}           
          }
          
          public int Iputbook(bookform w){
              bd database=new bd();
              Connection connect=database.getlink();
              Statement state=null;
              try {
                state=connect.createStatement();
               } catch (SQLException e) {System.out.print("sqlboom!
    ");}
              try {
                state.executeUpdate("INSERT INTO books VALUES('"+w.getCode()+"','"+w.getName()+"','"+w.getMaster()+"','"+w.getOutput()+"','"+w.getMaxamount()+"','0')");
                return 1;
               } catch (SQLException e) {System.out.print("insert error!
    ");return 0;}           
          }
          
          public bookform[] viewall() {
              bd database=new bd();
                  Connection connect=database.getlink();
                  Statement state=null;
                  try {
                    state=connect.createStatement();
                   } catch (SQLException e) {System.out.print("sqlboom!
    ");}
                  bookform[] pop=new bookform[1000];
                  for(int i=0;i<1000;i++) {
                      pop[i]=new bookform();
                  }
                  ResultSet rsp=null;
                  try {
               rsp = state.executeQuery("select * from books");
               } catch (SQLException e) {System.out.print("rs.error");}
                  try {
                for(int i=0;rsp.next();i++) {
                    pop[i].setCode(rsp.getString("code"));
                    pop[i].setName(rsp.getString("name"));   
                    pop[i].setMaster(rsp.getString("master"));  
                    pop[i].setOutput(rsp.getString("output"));  
                    pop[i].setAmount(Integer.parseInt(rsp.getString("amount")));   
                    pop[i].setMaxamount(Integer.parseInt(rsp.getString("maxamount")));   
                }
                } catch (SQLException e) {System.out.print("rsget.error");}
                  return pop;
         }
          
          public int select(String student,String code) throws SQLException {
              int i=1;
              LocalDate date = LocalDate.now();
              DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
              String day=date.format(formatter);
               bd database=new bd();
                  Connection connect=database.getlink();
                  Statement state=null;
               state=connect.createStatement();Statement st = connect.createStatement();
               ResultSet rs = state.executeQuery("select count(*) from seekbook where student='"+student+"' and book='"+code+"'");
               rs.next();
               int result = rs.getInt(1);
               rs.close();
               ResultSet rsp = st.executeQuery("select * from  books where code='"+code+"'");
               int num=0;int maxnum=0;
               if(rsp.next()) {
               num=Integer.parseInt(rsp.getString("amount"));
               maxnum=Integer.parseInt(rsp.getString("maxamount"));
               System.out.print(result);
               System.out.print(num);
               
               }
               if(result==0&&(num<maxnum)){
                   state.executeUpdate("INSERT INTO seekbook VALUES('"+student+code+"','"+code+"','"+student+"','"+day+"')");
                   num++;
                   state.executeUpdate("update books set amount='"+num+"' where code='"+code+"'");
               }else {
                   i=0;    
               }
               return i;
        }
         
          public bookform[] viewonly(String name,String master) {
                 bd database=new bd();
                     Connection connect=database.getlink();
                     Statement state=null;
                     try {
                       state=connect.createStatement();
                      } catch (SQLException e) {System.out.print("sqlboom!
    ");}
                     bookform[] pop=new bookform[1000];
                     for(int i=0;i<1000;i++) {
                         pop[i]=new bookform();
                     }
                     ResultSet rsp=null;
                     try {
                  rsp = state.executeQuery("select * from books where name like '%"+name+"%' and master like '%"+master+"%'");
                  } catch (SQLException e) {System.out.print("rs.error");}
                     try {
                   for(int i=0;rsp.next();i++) {
                       pop[i].setCode(rsp.getString("code"));
                       pop[i].setName(rsp.getString("name"));   
                       pop[i].setMaster(rsp.getString("master"));  
                       pop[i].setOutput(rsp.getString("output"));  
                       pop[i].setAmount(Integer.parseInt(rsp.getString("amount")));   
                       pop[i].setMaxamount(Integer.parseInt(rsp.getString("maxamount")));   
                   }
                   } catch (SQLException e) {System.out.print("rsget.error");}
                     return pop;
            }
          
          public int date(String day1,String day2) {
              String between_dayInteger = ""+between_days(day1, day2);
              return Integer.parseInt(between_dayInteger);
          }
          
          public int refuse(String code,String book) throws SQLException {
              bd database=new bd();
                  Connection connect=database.getlink();
                  Statement state=null;
               state=connect.createStatement();
               state.executeUpdate("delete  from seekbook where student='"+code+"' and book='"+book+"'");
               return 1;
          }
          public seekform[] viewme(String code) throws SQLException {
               bd database=new bd();
               seekform[] pop=new seekform[1000];
                  Connection connect=database.getlink();
                  Statement state=null;
               state=connect.createStatement();
               ResultSet rsp = state.executeQuery("select * from seekbook where student='"+code+"'");
               for(int i=0; i<1000; i++)
               {pop[i] = new seekform();}
               for(int i=0;rsp.next();i++) {
                    pop[i].setBook(rsp.getString("book"));
                    pop[i].setDay(rsp.getString("day"));
                    pop[i].setStudent(rsp.getString("student"));
                }
               return pop;
         }
         public seekform[] viewself(String code) throws SQLException {
               bd database=new bd();
               seekform[] pop=new seekform[1000];
                  Connection connect=database.getlink();
                  Statement state=null;
               state=connect.createStatement();
               ResultSet rsp = state.executeQuery("select * from seekbook where student='"+code+"'");
               for(int i=0; i<1000; i++)
               {pop[i] = new seekform();}
               for(int i=0;rsp.next();i++) {
                   LocalDate date = LocalDate.now();
                      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
                      String day=date.format(formatter);
                      if(date(rsp.getString("day"),day)>30) {
                    pop[i].setBook(rsp.getString("book"));
                    pop[i].setDay(rsp.getString("day"));
                    pop[i].setStudent(rsp.getString("student"));
                      }else {
                       i--;
                      }
                }
               return pop;
         }
         public seekform[] viewas() throws SQLException {
               bd database=new bd();
               seekform[] pop=new seekform[1000];
                  Connection connect=database.getlink();
                  Statement state=null;
               state=connect.createStatement();
               ResultSet rsp = state.executeQuery("select * from seekbook");
               for(int i=0; i<1000; i++)
               {pop[i] = new seekform();}
               for(int i=0;rsp.next();i++) {
                   LocalDate date = LocalDate.now();
                      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 
                      String day=date.format(formatter);
                      if(date(rsp.getString("day"),day)>30) {
                    pop[i].setBook(rsp.getString("book"));
                    pop[i].setDay(rsp.getString("day"));
                    pop[i].setStudent(rsp.getString("student"));
                      }else {
                       i--;
                      }
                }
               return pop;
         }
          
          
    }
  • 相关阅读:
    Python学习第75天(js历史和引入,模块复习)
    Python学习第74天(抽屉习题笔记)
    Python学习第73天(shelve模块、习题练习)
    Js查漏补缺02-各种数据类型
    Js查漏补缺01-js学习笔记
    开篇
    小小python欢乐多
    阅读笔记09 个人对于三年来软件工程的一点心得
    14周周博客
    软件杯第二阶段
  • 原文地址:https://www.cnblogs.com/lkwkk/p/14218603.html
Copyright © 2011-2022 走看看