d_borrow.java

来自「图书管理系统」· Java 代码 · 共 169 行

JAVA
169
字号
/*
 * D_borrow.java
 *
 * Created on 2007年6月22日, 上午9:21
 */

package 图书管理系统;
import javax.swing.*;
import java.sql.*;
/**
 *
 * @author  ERIC
 */
public class D_borrow extends javax.swing.JFrame {
    
    /** Creates new form D_borrow */
    public D_borrow() {
        initComponents();
        con=connect.getcon();
        stmt=connect.getstmt();
        this.setTitle("借书");
        this.setBounds(300,250,350,250);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    
    /** 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=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        T_RID = new javax.swing.JTextField();
        T_BID = new javax.swing.JTextField();
        B_add = new javax.swing.JButton();
        B_cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("\u8bfb\u8005\u7f16\u53f7:");

        jLabel2.setText("\u56fe\u4e66ID:");

        B_add.setText("\u501f\u4e66");
        B_add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_addActionPerformed(evt);
            }
        });

        B_cancel.setText("\u53d6\u6d88");
        B_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_cancelActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(60, 60, 60)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(B_add)
                        .add(54, 54, 54)
                        .add(B_cancel))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(35, 35, 35)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(T_BID)
                            .add(T_RID, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(41, 41, 41)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(T_RID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(32, 32, 32)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(T_BID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(24, 24, 24)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(B_add)
                    .add(B_cancel))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void B_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_cancelActionPerformed
// TODO 将在此处添加您的处理代码:
        this.setVisible(false);
    }//GEN-LAST:event_B_cancelActionPerformed

    private void B_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_addActionPerformed
// TODO 将在此处添加您的处理代码:
        try{
            String rid=T_RID.getText().trim();
            
            PreparedStatement   sql=con.prepareStatement("select 已借 from 读者 where 编号=?");
            //JOptionPane.showMessageDialog(null,"2");
            sql.setString(1,rid);
             
            ResultSet rs=sql.executeQuery();
            
            int n=0;
            rs.next();
             n=rs.getInt(1);
            n=n+1;
            if(n<=10)
           {
               
            sql=con.prepareStatement("update 读者 set 已借=? where 编号=?");
            
            sql.setInt(1,n);
            sql.setString(2,rid);
            sql.executeUpdate();
        }
            else
            {
                JOptionPane.showMessageDialog(null,"您所借的书的数量已经超限额");
                return;
            }
        sql=con.prepareStatement("insert into 借阅(书号,读者编号,借阅日期) values (?,?,getdate())");
        sql.setString(1,T_BID.getText().trim());
        sql.setString(2,rid);
        sql.executeUpdate();
        JOptionPane.showMessageDialog(null,"数据添加成功");
        this.setVisible(false);
    }//GEN-LAST:event_B_addActionPerformed
     catch(Exception ee)
         {
       JOptionPane.showMessageDialog(null,"数据库访问失败,请检查输入的字符是否正确");
 ee.printStackTrace();
    }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new D_borrow().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton B_add;
    private javax.swing.JButton B_cancel;
    private javax.swing.JTextField T_BID;
    private javax.swing.JTextField T_RID;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // 变量声明结束//GEN-END:variables
   private Connection con;
   private Statement stmt;
}

⌨️ 快捷键说明

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