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

📄 add_jieyue.java

📁 图书管理系统
💻 JAVA
字号:
/* * Add_Jieyue.java * * Created on 2008年5月19日, 上午10:45 */import java.sql.*;import javax.swing.JOptionPane;/** * * @author  Administrator */public class Add_Jieyue extends javax.swing.JDialog {        /** Creates new form Add_Jieyue */    public Add_Jieyue(java.awt.Frame parent, boolean modal) {        super(parent, modal);        initComponents();        this.setTitle("添加借阅记录");        this.setResizable(false);    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jLabel11 = new javax.swing.JLabel();        jPanel2 = new javax.swing.JPanel();        jLabel6 = new javax.swing.JLabel();        jLabel7 = new javax.swing.JLabel();        jLabel8 = new javax.swing.JLabel();        jtxtBorrowTime1 = new javax.swing.JTextField();        jLabel9 = new javax.swing.JLabel();        jLabel10 = new javax.swing.JLabel();        jbtnQuery1 = new javax.swing.JButton();        jlabelMsg = new javax.swing.JLabel();        jtxtIndex = new javax.swing.JTextField();        jcmbLibCard = new javax.swing.JComboBox();        jcmbISBN = new javax.swing.JComboBox();        jLabel11.setText("jLabel11");        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);        jLabel6.setText("借书证号:");        jLabel7.setText("ISBN:");        jLabel8.setText("借出时间:");        jLabel9.setText("(格式:2000-11-11)");        jLabel10.setText("索书号:");        jbtnQuery1.setText("添加");        jbtnQuery1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jbtnQuery1ActionPerformed(evt);            }        });        jlabelMsg.setFont(new java.awt.Font("宋体", 0, 14));        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);        jPanel2.setLayout(jPanel2Layout);        jPanel2Layout.setHorizontalGroup(            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel2Layout.createSequentialGroup()                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()                        .addGap(20, 20, 20)                        .addComponent(jlabelMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()                        .addGap(36, 36, 36)                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                            .addGroup(jPanel2Layout.createSequentialGroup()                                .addComponent(jLabel10)                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                .addComponent(jtxtIndex, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                .addComponent(jbtnQuery1))                            .addGroup(jPanel2Layout.createSequentialGroup()                                .addComponent(jLabel8)                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                .addComponent(jtxtBorrowTime1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                .addComponent(jLabel9))                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()                                    .addComponent(jLabel7)                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                                    .addComponent(jcmbISBN, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()                                    .addComponent(jLabel6)                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                    .addComponent(jcmbLibCard, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))))                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))                .addContainerGap(11, Short.MAX_VALUE))        );        jPanel2Layout.setVerticalGroup(            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel2Layout.createSequentialGroup()                .addGap(35, 35, 35)                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel6)                    .addComponent(jcmbLibCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addGap(18, 18, 18)                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel7)                    .addComponent(jcmbISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel8)                    .addComponent(jtxtBorrowTime1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jLabel9))                .addGap(18, 18, 18)                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel10)                    .addComponent(jtxtIndex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                    .addComponent(jbtnQuery1))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jlabelMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))        );        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);        pack();    }// </editor-fold>//GEN-END:initComponents    public void Empty()    {        try{            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");        }catch(ClassNotFoundException ex)        {            jlabelMsg.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 rs1=st.executeQuery("select libCard from XS");            jcmbLibCard.removeAllItems();            while(rs1.next()) {                 jcmbLibCard.addItem(rs1.getString("libCard"));            }            ResultSet res=st.executeQuery("select ISBN from book");            jcmbISBN.removeAllItems();            while(res.next()) {                 jcmbISBN.addItem(res.getString("ISBN"));            }            try {                if (con != null) { con.close(); }            } catch (SQLException sQLException) { }        }        catch(Exception e){            JOptionPane.showConfirmDialog(null,"找不到数据库驱动程序!","Warning",JOptionPane.CLOSED_OPTION,JOptionPane.WARNING_MESSAGE);            this.setVisible(false);        }        jcmbLibCard.setSelectedIndex(0);        jcmbISBN.setSelectedIndex(0);        jtxtBorrowTime1.setText("");        jlabelMsg.setText("输入借阅所有记录,然后点击添加。");    }        private void jbtnQuery1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnQuery1ActionPerformed        // TODO add your handling code here:        String lc="",is="",bt="",ss="";        int kucun=0;        try{            lc=new String((String)jcmbLibCard.getSelectedItem());            if(lc.length()<1) throw new ArithmeticException();            is=new String((String)jcmbISBN.getSelectedItem());            if(is.length()<1) throw new ArithmeticException();            bt=new String(jtxtBorrowTime1.getText().trim());            if(bt.length()<1||bt.length()>10) throw new ArithmeticException();            ss=new String(jtxtIndex.getText().trim());            if(ss.length()<0||ss.length()>10) throw new ArithmeticException();                        try{            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");            }catch(ClassNotFoundException ex){                throw new ClassNotFoundException();            }            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 jy");            while(rs.next())                if(ss.equals(rs.getString("suoshuNo"))) throw new ArrayIndexOutOfBoundsException();            PreparedStatement ps3=con.prepareStatement("select kucunNum from book where isbn=?");            ps3.setString(1, is);            ResultSet rs3=ps3.executeQuery();            while(rs3.next()) kucun=new Integer(Integer.parseInt(rs3.getString("kucunNum")));            if(kucun<1) throw new ArithmeticException();                        PreparedStatement ps=con.prepareStatement("insert into jy values(?,?,?,?)");            ps.setString(1, ss);            ps.setString(2, lc);            ps.setString(3, is);            ps.setString(4, bt);            try {                ps.executeUpdate();            } catch (SQLException sQLException) {                throw new NumberFormatException();            }            //添加借阅记录时,对应的读者借书数加1,书的库存量减1            PreparedStatement ps2=con.prepareStatement("update xs set borrowNum=borrowNum+1 where libCard=?");            ps2.setString(1, lc);            ps2.executeUpdate();            PreparedStatement ps4=con.prepareStatement("update book set kucunNum=KucunNum-1 where ISBN=?");            ps4.setString(1, is);            ps4.executeUpdate();            jlabelMsg.setText("添加成功!");            try {                if (con != null) { con.close(); }            } catch (SQLException sQLException) {            }        }catch(ArithmeticException e){            if(lc.length()<1) jlabelMsg.setText("图书证号为空!");            else if(is.length()<1) jlabelMsg.setText("ISBN为空!");            else if(bt.length()<1) jlabelMsg.setText("借书时间不能为空!");            else if(bt.length()>10) jlabelMsg.setText("借书时间格式不正确!");            else if(ss.length()<1) jlabelMsg.setText("索书号不能为空!");            else  if(ss.length()>10) jlabelMsg.setText("索书号格式不正确!");            else if(kucun<1) jlabelMsg.setText("此书已借完!");        }catch(ClassNotFoundException ex){            jlabelMsg.setText("SQL错:"+ex.getMessage());        }catch(ArrayIndexOutOfBoundsException e){            jlabelMsg.setText("此索书号借阅记录已存在!");        }catch(NumberFormatException ex) {            jlabelMsg.setText("借书日期格式不正确!");        }        catch(Exception e){            jlabelMsg.setText("插入失败!(可能连接数据库失败)");        }    }//GEN-LAST:event_jbtnQuery1ActionPerformed        /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                Add_Jieyue dialog = new Add_Jieyue(new javax.swing.JFrame(), true);                dialog.addWindowListener(new java.awt.event.WindowAdapter() {                    public void windowClosing(java.awt.event.WindowEvent e) {                        System.exit(0);                    }                });                dialog.setVisible(true);            }        });    }        // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JLabel jLabel10;    private javax.swing.JLabel jLabel11;    private javax.swing.JLabel jLabel6;    private javax.swing.JLabel jLabel7;    private javax.swing.JLabel jLabel8;    private javax.swing.JLabel jLabel9;    private javax.swing.JPanel jPanel2;    private javax.swing.JButton jbtnQuery1;    private javax.swing.JComboBox jcmbISBN;    private javax.swing.JComboBox jcmbLibCard;    private javax.swing.JLabel jlabelMsg;    private javax.swing.JTextField jtxtBorrowTime1;    private javax.swing.JTextField jtxtIndex;    // End of variables declaration//GEN-END:variables    }

⌨️ 快捷键说明

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