readerclass.java

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

JAVA
261
字号
/*
 * readerclass.java
 *
 * Created on 2007年6月24日, 上午7:42
 */

package 图书管理系统;
import javax.swing.*;
import java.sql.*;
public class readerclass extends javax.swing.JFrame {
    
    /** Creates new form readerclass */
    public readerclass() {
        initComponents();
        con=connect.getcon();
        T_class.setEnabled(false);
        B_stat.setEnabled(false);
        B_cancel.setEnabled(false);
        BG.add(BR_rclass);
        BG.add(RB_all);
        this.setTitle("读者信息统计");
        this.setBounds(300,250,500,400);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
           try{
        PreparedStatement sql=con.prepareStatement("select distinct 单位,count(编号) from 读者 group by 单位");
        rs = sql.executeQuery();
        sql=con.prepareStatement("select count(编号) from 读者");
        ResultSet rs2=sql.executeQuery();
        rs2.next();
        String s2=rs2.getString(1);
        String[] rowline={"总计",s2};
        int rows=0,column=0;
        ResultSetMetaData reMeta;
        int i=0,j=0,n=0;
        reMeta=rs.getMetaData();        
        CustomTableModel model=null;
        model=new CustomTableModel(0,name.length,name,dataType);
        rtable.setModel(model);
                  int p=reMeta.getColumnCount();
                    column=p;
                    String[] row=new String[p]; 
                    while(rs.next())
                   { 
                        
                       for(j=0;j<p;j++)
                       {
                        row[j]=rs.getString(j+1);
                        }
                       model.insertRow(i,row);
                       i++;
                        }
                    model.insertRow(i,rowline);
                    rtable.repaint();
                    this.validate();   
        }
        catch(Exception ee)
         {
       JOptionPane.showMessageDialog(null,"数据库访问失败!");
 ee.printStackTrace();
    }
    }
    
    /** 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() {
        BG = new javax.swing.ButtonGroup();
        T_class = new javax.swing.JTextField();
        BR_rclass = new javax.swing.JRadioButton();
        RB_all = new javax.swing.JRadioButton();
        B_stat = new javax.swing.JButton();
        B_cancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        rtable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BR_rclass.setText("\u4ece\u6587\u672c\u6846\u4e2d\u8f93\u5165\u8981\u7edf\u8ba1\u7684\u5355\u4f4d");
        BR_rclass.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        BR_rclass.setMargin(new java.awt.Insets(0, 0, 0, 0));
        BR_rclass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BR_rclassActionPerformed(evt);
            }
        });

        RB_all.setText("\u7edf\u8ba1\u6240\u6709\u8bfb\u8005\u4fe1\u606f");
        RB_all.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        RB_all.setMargin(new java.awt.Insets(0, 0, 0, 0));
        RB_all.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RB_allActionPerformed(evt);
            }
        });

        B_stat.setText("\u7edf\u8ba1");
        B_stat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_statActionPerformed(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);
            }
        });

        rtable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(rtable);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(BR_rclass)
                    .add(RB_all))
                .add(20, 20, 20)
                .add(T_class, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 79, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(B_stat)
                    .add(B_cancel))
                .add(68, 68, 68))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(BR_rclass)
                    .add(T_class, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(B_stat))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(RB_all)
                    .add(B_cancel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 22, Short.MAX_VALUE)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 289, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        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_statActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_statActionPerformed
// TODO 将在此处添加您的处理代码:
        try{
        PreparedStatement sql=con.prepareStatement("select 单位,count(编号) from 读者 where 单位=? group by 单位");
        sql.setString(1,T_class.getText().trim());
        rs=sql.executeQuery();
        PaintTable paint=new PaintTable(rs,name,rtable,this,dataType);
        }
        catch(Exception ee)
         {
       JOptionPane.showMessageDialog(null,"数据库访问失败!");
 ee.printStackTrace();
    }
    }//GEN-LAST:event_B_statActionPerformed

    private void BR_rclassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BR_rclassActionPerformed
// TODO 将在此处添加您的处理代码:
        T_class.setEnabled(true);
        B_stat.setEnabled(true);
        B_cancel.setEnabled(true);
    }//GEN-LAST:event_BR_rclassActionPerformed

    private void RB_allActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RB_allActionPerformed
// TODO 将在此处添加您的处理代码:
        T_class.setEnabled(false);
        B_stat.setEnabled(false);
        B_cancel.setEnabled(false);
        try{
        PreparedStatement sql=con.prepareStatement("select distinct 单位,count(编号) from 读者 group by 单位");
        rs = sql.executeQuery();
        sql=con.prepareStatement("select count(编号) from 读者");
        ResultSet rs2=sql.executeQuery();
        rs2.next();
        String s2=rs2.getString(1);
        String[] rowline={"总计",s2};
        int rows=0,column=0;
        ResultSetMetaData reMeta;
        int i=0,j=0,n=0;
        reMeta=rs.getMetaData();        
        CustomTableModel model=null;
        model=new CustomTableModel(0,name.length,name,dataType);
        rtable.setModel(model);
                  int p=reMeta.getColumnCount();
                    column=p;
                    String[] row=new String[p]; 
                    while(rs.next())
                   { 
                        
                       for(j=0;j<p;j++)
                       {
                        row[j]=rs.getString(j+1);
                        }
                       model.insertRow(i,row);
                       i++;
                        }
                    model.insertRow(i,rowline);
                    rtable.repaint();
                    this.validate();   
        }
        catch(Exception ee)
         {
       JOptionPane.showMessageDialog(null,"数据库访问失败!");
 ee.printStackTrace();
    }
    }//GEN-LAST:event_RB_allActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new readerclass().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.ButtonGroup BG;
    private javax.swing.JRadioButton BR_rclass;
    private javax.swing.JButton B_cancel;
    private javax.swing.JButton B_stat;
    private javax.swing.JRadioButton RB_all;
    private javax.swing.JTextField T_class;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable rtable;
    // 变量声明结束//GEN-END:variables
    private Connection con;
    private Statement stmt;
   private String[] name={"单位","人数"};
   private String sql;
   private Class[] dataType={String.class,String.class};
   private ResultSet rs;
}

⌨️ 快捷键说明

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