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

📄 add_tushu.java

📁 图书管理系统
💻 JAVA
字号:
import java.sql.*;/* * Add_Tushu.java * * Created on 2008年5月18日, 下午1:31 *//** * * @author  Administrator */public class Add_Tushu extends javax.swing.JDialog {        /** Creates new form Add_Tushu */    public Add_Tushu(java.awt.Frame parent, boolean modal) {        super(parent, modal);        initComponents();        this.setTitle("添加图书信息");        this.setLocation(200,220);        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() {        jPanel1 = new javax.swing.JPanel();        jLabel1 = new javax.swing.JLabel();        jtxtName = new javax.swing.JTextField();        jLabel2 = new javax.swing.JLabel();        jtxtAuthor = new javax.swing.JTextField();        jLabel3 = new javax.swing.JLabel();        jtxtPublish = new javax.swing.JTextField();        jLabel4 = new javax.swing.JLabel();        jtxtFuben = new javax.swing.JTextField();        jLabel5 = new javax.swing.JLabel();        jtxtKucun = new javax.swing.JTextField();        jLabel6 = new javax.swing.JLabel();        jtxtISBN = new javax.swing.JTextField();        jbtnAdd = new javax.swing.JButton();        jlabelMsg = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);        setTitle("添加图书");        addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosed(java.awt.event.WindowEvent evt) {                formWindowClosed(evt);            }        });        jLabel1.setText("书名: ");        jLabel2.setText("作者: ");        jLabel3.setText("出版社:");        jLabel4.setText("副本量:");        jtxtFuben.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jtxtFubenActionPerformed(evt);            }        });        jLabel5.setText("库存量:");        jtxtKucun.setEnabled(false);        jLabel6.setText("ISBN:");        jbtnAdd.setText("添加");        jbtnAdd.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jbtnAddActionPerformed(evt);            }        });        jlabelMsg.setFont(new java.awt.Font("宋体", 0, 16));        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);        jPanel1.setLayout(jPanel1Layout);        jPanel1Layout.setHorizontalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()                        .addContainerGap()                        .addComponent(jlabelMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))                    .addGroup(jPanel1Layout.createSequentialGroup()                        .addGap(24, 24, 24)                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                                .addGroup(jPanel1Layout.createSequentialGroup()                                    .addComponent(jLabel3)                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                    .addComponent(jtxtPublish, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))                                .addGroup(jPanel1Layout.createSequentialGroup()                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)                                        .addComponent(jLabel1)                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                                        .addComponent(jtxtName, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)                                        .addComponent(jtxtAuthor, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))))                            .addGroup(jPanel1Layout.createSequentialGroup()                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)                                    .addComponent(jLabel4)                                    .addComponent(jLabel6))                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                                    .addGroup(jPanel1Layout.createSequentialGroup()                                        .addComponent(jtxtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                        .addComponent(jbtnAdd))                                    .addGroup(jPanel1Layout.createSequentialGroup()                                        .addComponent(jtxtFuben, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)                                        .addGap(2, 2, 2)                                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                                        .addComponent(jtxtKucun)))))                        .addGap(9, 9, 9)))                .addGap(47, 47, 47))        );        jPanel1Layout.setVerticalGroup(            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(jPanel1Layout.createSequentialGroup()                .addGap(27, 27, 27)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel1)                    .addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel2)                    .addComponent(jtxtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel3)                    .addComponent(jtxtPublish, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addComponent(jLabel4)                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                        .addComponent(jtxtFuben, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addComponent(jLabel5)                        .addComponent(jtxtKucun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))                .addGap(18, 18, 18)                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)                    .addComponent(jLabel6)                    .addComponent(jbtnAdd)                    .addComponent(jtxtISBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                .addComponent(jlabelMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)                .addContainerGap(18, Short.MAX_VALUE))        );        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);        pack();    }// </editor-fold>//GEN-END:initComponents    public void Empty()    {        jtxtISBN.setText("");        jtxtName.setText("");        jtxtAuthor.setText("");        jtxtPublish.setText("");        jtxtFuben.setText("");        jtxtKucun.setText("");        jlabelMsg.setText("输入图书所有信息,然后点击添加。");    }        private void jbtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddActionPerformed        // TODO add your handling code here:        String is="",na="",au="",pu="";        int fu=0,ku=0;        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);            is=new String(jtxtISBN.getText().trim());            if(is.length()!=13) throw new ArithmeticException();            na=new String(jtxtName.getText().trim());            if(na.length()<1) throw new ArithmeticException();            au=new String(jtxtAuthor.getText().trim());            if(au.length()<1) throw new ArithmeticException();            pu=new String(jtxtPublish.getText().trim());            if(pu.length()<1) throw new ArithmeticException();            try{                fu=new Integer(Integer.parseInt(jtxtFuben.getText().trim()));            }catch(NumberFormatException e){                throw new NumberFormatException();            }            if(fu>2147483647||fu<0) throw new ArithmeticException();            ku=fu;            if(ku>2147483647||ku<0) throw new ArithmeticException();            else if(ku!=fu) throw new ArithmeticException();                        Statement st=con.createStatement();            ResultSet rs=st.executeQuery("select * from book");            while(rs.next()){                if(is.equals(rs.getString("ISBN"))) throw new ArrayIndexOutOfBoundsException();            }            PreparedStatement ps=con.prepareStatement("insert into book values(?,?,?,?,?,?)");            ps.setString(1, is);            ps.setString(2, na);            ps.setString(3, au);            ps.setString(4, pu);            ps.setInt(5, fu);            ps.setInt(6,ku);            ps.executeUpdate();            jlabelMsg.setText("添加图书成功!");            if(con!=null) con.close();        }        catch(ArithmeticException e){            if(is.length()<1) jlabelMsg.setText("ISBN号不能为空!");            else if(is.length()!=13) jlabelMsg.setText("ISBN格式不正确(13位)!");            else if(na.length()<1) jlabelMsg.setText("书名不能为空!");            else if(au.length()<1) jlabelMsg.setText("作者不能为空!");            else if(pu.length()<1) jlabelMsg.setText("出版社不能为空!");            else if(fu>2147483647||fu<0) jlabelMsg.setText("副本量错误!");            else if(ku>2147483647||ku<0) jlabelMsg.setText("库存量错误");            else if(ku>fu) jlabelMsg.setText("副本量和库存量不等!");        }        catch(ArrayIndexOutOfBoundsException e) {            jlabelMsg.setText("此ISBN号书已存在!");        }        catch(NumberFormatException e) {            jlabelMsg.setText("副本量格式不正确!");        }        catch(Exception e){            jlabelMsg.setText("插入失败!(可能连接数据库失败)");        }            }//GEN-LAST:event_jbtnAddActionPerformed        private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed        // TODO add your handling code here:    }//GEN-LAST:event_formWindowClosedprivate void jtxtFubenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtFubenActionPerformed// TODO add your handling code here:    jtxtKucun.setText(jtxtFuben.getText());}//GEN-LAST:event_jtxtFubenActionPerformed        /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                Add_Tushu dialog = new Add_Tushu(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 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.JPanel jPanel1;    private javax.swing.JButton jbtnAdd;    private javax.swing.JLabel jlabelMsg;    private javax.swing.JTextField jtxtAuthor;    private javax.swing.JTextField jtxtFuben;    private javax.swing.JTextField jtxtISBN;    private javax.swing.JTextField jtxtKucun;    private javax.swing.JTextField jtxtName;    private javax.swing.JTextField jtxtPublish;    // End of variables declaration//GEN-END:variables    }

⌨️ 快捷键说明

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