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

📄 modify_duzhe.java

📁 图书管理系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch(ClassNotFoundException ex)
        {
            jlabelMessage.setText("SQL错:"+ex.getMessage());
        }
        try {
            String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\data.mdb;pwd=shujuyuan";
            Connection con=DriverManager.getConnection(url);
            Statement st=con.createStatement();            
            ResultSet res=st.executeQuery("select libCard from XS");
            jcmblibCard.removeAllItems();
            while(res.next()) {
                 jcmblibCard.addItem(res.getString("libCard"));
            }
            st.close();
        } catch(Exception ex){
            JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);
            this.setVisible(false);
        }
        jcmblibCard.setSelectedItem(null);
        jtxtName.setText("");
        jcmbGender.setSelectedItem(null);
        jtxtBirth.setText("");
        jtxtMajor.setText("");
        jtxtBorrowNum.setText("");
        jlabelMessage.setText("输入读者所有信息,然后点击修改。");
    }
    private void jbtnQuery_DuzheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnQuery_DuzheActionPerformed
// TODO 将在此处添加您的处理代码
        int bn=0;
        String flag="",lc="",na="",mj="",sex="",bi="";
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }catch(ClassNotFoundException ex) {
           jlabelMessage.setText("SQL错:"+ex.getMessage());
        }
        try{           //修改方法是先删除,后添加
//            Connection con=DriverManager.getConnection("jdbc:odbc:XSBOOK");
            String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\data.mdb;pwd=shujuyuan";
            Connection con=DriverManager.getConnection(url);
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from XS");
            try{
                lc=new String((String)jcmblibCard.getSelectedItem());
            }catch(NullPointerException e){
                throw new NullPointerException();
            }
            if(lc.length()!=8) throw new ArithmeticException();
            na=new String(jtxtName.getText());
            if(na.length()>8||na.length()<1) throw new ArithmeticException();
            sex=new String((String)jcmbGender.getSelectedItem());
            if(sex.length()>2||sex.length()<1) throw new ArithmeticException();
            bi=new String(jtxtBirth.getText());
            if(bi.length()<1||bi.length()>10) throw new ArithmeticException();
            mj=new String(jtxtMajor.getText());
            if(mj.length()>12||mj.length()<1) throw new ArithmeticException();
            
            while(rs.next())
                if(rs.getString("libCard").equals(lc))
                {
                    flag=new String(rs.getString("name"));
                }
            if(flag.length()<1) throw new ArrayIndexOutOfBoundsException();
            PreparedStatement ps_del=con.prepareStatement("delete from xs where libCard=?");
            ps_del.setString(1,lc);
            ps_del.executeUpdate();
            //以上为删除部分
            PreparedStatement ps_add=con.prepareStatement("insert into XS values(?,?,?,?,?,?)");
            ps_add.setString(1,lc);
            ps_add.setString(2,na);
            ps_add.setString(3,mj);
            ps_add.setString(4,sex);
            ps_add.setString(5,bi);
            ps_add.setInt(6,bn);
            ps_add.executeUpdate();
            //以上为插入部分
            jlabelMessage.setText("修改成功!");
        }
        catch(ArrayIndexOutOfBoundsException ex){
            jlabelMessage.setText("此图书证号码不存在!");
        }
        catch(NullPointerException ex){
            jlabelMessage.setText("请选择图书证号!");
        }
        catch(ArithmeticException ex){
             if(lc.length()<1) jlabelMessage.setText("图书证号码不能为空!");
             else if(lc.length()!=8) jlabelMessage.setText("图书证号码不是8位!");
             else if(na.length()<1) jlabelMessage.setText("名字不能为空!");
             else if(na.length()>8) jlabelMessage.setText("名字太长!");
             else if(sex.length()<1) jlabelMessage.setText("性别不能为空!");
             else if(sex.length()>2) jlabelMessage.setText("性别太长!");
             else if(bi.length()<1) jlabelMessage.setText("出生时间不能为空!");
             else if(bi.length()>10) jlabelMessage.setText("出生时间格式不正确!");
             else if(mj.length()<1) jlabelMessage.setText("专业名不能为空!");
             else if(mj.length()>12) jlabelMessage.setText("专业名太长!");
             else if(bn>2147483647) jlabelMessage.setText("借书数太多!");
        }
        catch(Exception ex) {
            jlabelMessage.setText("修改失败!(可能连接数据库失败)");
        }
    }//GEN-LAST:event_jbtnQuery_DuzheActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void jcmblibCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmblibCardActionPerformed
        // TODO add your handling code here:        
        jlabelMessage.setText("输入读者借书证号,然后点击查询。");
       try{
//            Connection con=DriverManager.getConnection("jdbc:odbc:xsbook");
            String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=sources\\data.mdb;pwd=shujuyuan";
            Connection con=DriverManager.getConnection(url);
            PreparedStatement ps=con.prepareStatement("select * from XS where libCard=?");
            String lc="";
            lc=new String((String)jcmblibCard.getSelectedItem());
            if(lc.length()<1) throw new ArrayIndexOutOfBoundsException();
            ps.setString(1,lc);
            ResultSet rs=ps.executeQuery();
            while(rs.next())
            {
                jtxtName.setText(new String(rs.getString("name")));
                jtxtMajor.setText(rs.getString("major"));
                jcmbGender.setSelectedItem(rs.getString("gender"));
                jtxtBirth.setText(rs.getString("birth").substring(0, 10));
                jtxtBorrowNum.setText(rs.getString("borrowNum"));
            }
            ps.close();
            con.close();
       } catch(Exception ex) {
             jlabelMessage.setText("ERROR:可能连接数据库失败!");
        }                
    }//GEN-LAST:event_jcmblibCardActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Modify_Duzhe(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnQuery_Duzhe;
    private javax.swing.JComboBox jcmbGender;
    private javax.swing.JComboBox jcmblibCard;
    private javax.swing.JLabel jlabelMessage;
    private javax.swing.JTextField jtxtBirth;
    private javax.swing.JTextField jtxtBorrowNum;
    private javax.swing.JTextField jtxtMajor;
    private javax.swing.JTextField jtxtName;
    // End of variables declaration//GEN-END:variables
    
}

⌨️ 快捷键说明

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