⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readercondb.java

📁 用java实现的一个应用程序,源码非常完整,可以直接运行
💻 JAVA
字号:
package 毕业设计;
import java.sql.*;
import javax.swing.JOptionPane;
import java.util.Vector;
public class ReaderConDB {
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private PreparedStatement pst;
    public ReaderConDB() {
        //***************************连接数据库***********************************
         try {
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         } catch (ClassNotFoundException ex) {
             System.out.println("Driver 出错");
         }
        try {
            String url = "jdbc:odbc:chenhaiLibrary";
            con = DriverManager.getConnection(url);
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                     ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex1) {
            System.out.println("lib 出错");
        }
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //***************************查询所有读者***********************************
     public Vector SearchAll() {
         Vector vt = new Vector();
         try {
             String str = "select * from Reader";
             rs = st.executeQuery(str);
             while (rs.next()) {
                 Vector tempvt = new Vector();
                 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                     tempvt.add(rs.getString(i));
                 }
                 vt.add(tempvt);
             }
             rs.close();
         } catch (SQLException ex) {
         }
         return vt;
     }

    //****************************查询前50名读者**********************************
     public Vector SearchTop() {
         Vector vt = new Vector();
         int k = 1;
         try {
             String str =
                     "select TOP 50 * from Reader order by BorrowCount DESC";
             rs = st.executeQuery(str);
             while (rs.next()) {
                 Vector tempvt = new Vector();
                 tempvt.add(String.valueOf(k++));
                 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                     tempvt.add(rs.getString(i));
                 }
                 vt.add(tempvt);
             }
             rs.close();
         } catch (SQLException ex) {
         }
         return vt;
     }

    //***************************查询要更新的读者***********************************
     public Vector SearchUpdateReader(String content, String sort,
                                      boolean isnot) {
         Vector vt = new Vector();
         String str = "";
         if (isnot) {
             if (sort.equals("读者卡号")) {
                 str = "select * from Reader where Id = '" + content + "'";
             } else if (sort.equals("读者姓名")) {
                 str = "select * from Reader where Name = '" + content + "'";
             } else if (sort.equals("读者职业")) {
                 str = "select * from Reader where Vocation = '" + content +
                       "'";
             }
         } else {
             if (sort.equals("读者卡号")) {
                 str = "select * from Reader where Id like '%" + content + "%'";
             } else if (sort.equals("读者姓名")) {
                 str = "select * from Reader where Name like '%" + content +
                       "%'";
             } else if (sort.equals("读者职业")) {
                 str = "select * from Reader where Vocation like '%" + content +
                       "%'";
             }
         }
         try {
             rs = st.executeQuery(str);
             while (rs.next()) {
                 Vector tempvt = new Vector();
                 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                     tempvt.add(rs.getString(i));
                 }
                 vt.add(tempvt);
             }
             rs.close();
         } catch (SQLException ex) {
         }
         return vt;
     }

    //****************************查询读者**********************************
     public Vector SearchReader(String Id, String Name, String Vocation,
                                boolean Isnot) {
         Vector vt = new Vector();
         String str = "";
         if (Isnot) {
             for (int i = 0; i < 3; i++) {
                 String tem = "";
                 if (i == 0) {
                     if (!Id.equals("")) {
                         tem = " and Id = '" + Id + "'";
                     }
                 } else if (i == 1) {
                     if (!Name.equals("")) {
                         tem = " and Name = '" + Name + "'";
                     }
                 } else if (i == 2) {
                     if (!Vocation.equals("")) {
                         tem = " and Vocation = '" + Vocation + "'";
                     }
                 }
                 if (!tem.equals("")) {
                     str = str + tem;
                 }
             }
         } else {
             for (int i = 0; i < 3; i++) {
                 String tem = "";
                 if (i == 0) {
                     if (!Id.equals("")) {
                         tem = " and Id like '%" + Id + "%'";
                     }
                 } else if (i == 1) {
                     if (!Name.equals("")) {
                         tem = " and Name like '%" + Name + "%'";
                     }
                 } else if (i == 2) {
                     if (!Vocation.equals("")) {
                         tem = " and Vocation like '%" + Vocation + "%'";
                     }
                 }
                 if (!tem.equals("")) {
                     str = str + tem;
                 }
             }
         }
         try {
             String strSQL = "select * from Reader where 1=1" + str;
             rs = st.executeQuery(strSQL);
             while (rs.next()) {
                 Vector tempvt = new Vector();
                 for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                     tempvt.add(rs.getString(i));
                 }
                 vt.add(tempvt);
             }
             rs.close();
         } catch (SQLException ex) {
         }
         return vt;
     }

    //*******************************查询借书还书读者*******************************
     public Vector SearchLRReader(String Id) {
         Vector vt = new Vector();
         try {
             String strSQL = "select * from Reader where Id = '" + Id + "'";
             rs = st.executeQuery(strSQL);
             while (rs.next()) {
                 Reader rd = new Reader();
                 rd.setId(rs.getString(1));
                 rd.setName(rs.getString(2));
                 rd.setSex(rs.getString(3));
                 rd.setBirthTime(rs.getString(4));
                 rd.setVocation(rs.getString(5));
                 rd.setIdentityCard(rs.getString(6));
                 rd.setTel(rs.getString(7));
                 rd.setLoanableNum(rs.getString(8));
                 rd.setAlreadyNum(rs.getString(9));
                 rd.setTransactTime(rs.getString(10));
                 rd.setEndTime(rs.getString(11));
                 rd.setBorrowCount(rs.getString(12));
                 rd.setAddress(rs.getString(13));
                 rd.setRemark(rs.getString(14));
                 vt.add(rd);
             }
             rs.close();
         } catch (SQLException ex) {
         }
         return vt;
     }

    //****************************借书时更新读者信息****************************
     public void UpdateBorrowBook(String Id) {
         try {
             String strSQL = "update Reader set LoanableNum = LoanableNum - 1, AlreadyNum = AlreadyNum + 1, BorrowCount = BorrowCount + 1 where Id = '" +
                             Id + "'";
             pst = con.prepareStatement(strSQL);
             pst.executeUpdate();
             pst.close();
         } catch (SQLException ex) {
         }
     }

    //***************************还书时更新读者信息****************************
     public void UpdateReturnBook(String Id) {
         try {
             String strSQL = "update Reader set LoanableNum = LoanableNum + 1, AlreadyNum = AlreadyNum - 1 where Id = '" +
                             Id + "'";
             pst = con.prepareStatement(strSQL);
             pst.executeUpdate();
             pst.close();
         } catch (SQLException ex) {
         }
     }

    //****************************更新读者信息**********************************
     public boolean UpdateReader(String Id, String Name, String Sex,
                                 String BirthTime,
                                 String Vocation, String IdentityCard,
                                 String Tel,
                                 String LoanableNum, String AlreadyNum,
                                 String TransactTime,
                                 String EndTime, String BorrowCount,
                                 String Address, String Remark) {
         boolean Success = true;
         try {
             String strSQL = "update Reader set Name = '" + Name + "', Sex = '" +
                             Sex +
                             "', BirthTime = '" + BirthTime + "', Vocation = '" +
                             Vocation +
                             "', IdentityCard = '" + IdentityCard +
                             "', Tel = '" +
                             Tel +
                             "', LoanableNum = '" + LoanableNum +
                             "', AlreadyNum = '" +
                             AlreadyNum + "', TransactTime = '" + TransactTime +
                             "', EndTime = '" + EndTime + "', BorrowCount = '" +
                             BorrowCount + "', Address = '" + Address +
                             "', Remark = '" +
                             Remark + "' where Id = '" + Id + "'";
             pst = con.prepareStatement(strSQL);
             pst.executeUpdate();
             pst.close();
         } catch (SQLException ex) {
             Success = false;
         }
         return Success;
     }

    //***************************添加读者***********************************
     public boolean AddReader(String Id, String Name, String Sex, String BirthTime,
                           String Vocation, String TransactTime, String EndTime,
                           String IdentityCard, String Tel, String LoanableNum,
                           String Address, String Remark) {
      boolean Success = true;
         try {
             String strSQL = "insert Reader values ('" + Id + "', '" + Name +
                             "', '" + Sex +
                             "', '" + BirthTime + "','" + Vocation + "', '" +
                             IdentityCard +
                             "', '" + Tel + "', " + LoanableNum + ", 0, '" +
                             TransactTime +
                             "', '" + EndTime + "', 0, '" + Address + "', '" +
                             Remark + "')";
             pst = con.prepareStatement(strSQL);
             pst.executeUpdate();
             pst.close();
         } catch (SQLException ex) {
             Success = false;
         }
         return Success;
     }

    //****************************删除读者**********************************
     public void DeleteReader(String Id) {
         try {
             String strSQL = "delete from Reader where Id = '" + Id + "'";
             pst = con.prepareStatement(strSQL);
             pst.executeUpdate();
             pst.close();
         } catch (SQLException ex) {
         }
     }

    //****************************关闭数据库**********************************
     public void CloseReaderDB() {
         try {
             st.close();
             con.close();
         } catch (SQLException ex) {
         }
     }

    private void jbInit() throws Exception {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -