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

📄 classmng.java

📁 在管理员表中初始插入数据 usn: admin, pwd: 21232F297A57A5A743894A0E4A801FC3 初始用户账号/密码: admin (密码为 md5 加密) SRC
💻 JAVA
字号:
import java.awt.*;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ClassMNG extends JDialog {
    FlowLayout flowLayout1 = new FlowLayout();
    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JButton jButton1 = new JButton();
    JPanel jPanel2 = new JPanel();
    JButton jButton2 = new JButton();
    JLabel jLabel1 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JScrollPane jScrollPane1 = new JScrollPane();
    AbstractTableModel2 dataModel1 = new AbstractTableModel2();
    JTable jTable1 = new JTable(dataModel1);
    JButton jButton3 = new JButton();
    FrmMain parent;
    
    public ClassMNG(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
    	Gload.checkLogin(this);
        this.parent = (FrmMain)owner;
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        try {
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            jbInit();
            this.setBounds((Gload.de.width - 340)/2, (Gload.de.height-360)/2, 340, 360);
            this.setResizable(false);

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        this.initClass();
    }

    public ClassMNG() {
        this(new Frame(), "ClassMNG", false);
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        jButton2.setBounds(new Rectangle(236, 7, 93, 25));
        jButton2.setText("添加新类");
        jButton2.addActionListener(new ClassMNG_jButton2_actionAdapter(this));
        jPanel2.setLayout(null);
        jLabel1.setText("类名:");
        jLabel1.setBounds(new Rectangle(3, 14, 33, 16));
        jTextField1.setText("");
        jTextField1.setBounds(new Rectangle(34, 8, 198, 25));
        jScrollPane1.getViewport().setBackground(Color.lightGray);
        jScrollPane1.setBounds(new Rectangle(5, 38, 322, 279));
        jButton1.setToolTipText("");
        jButton1.addActionListener(new ClassMNG_jButton1_actionAdapter(this));
        jButton3.setText("编辑类名");
        jButton3.addActionListener(new ClassMNG_jButton3_actionAdapter(this));
        this.setDefaultCloseOperation(javax.swing.WindowConstants.
                                      DISPOSE_ON_CLOSE);
        jPanel2.add(jButton2);
        jPanel2.add(jTextField1);
        jPanel2.add(jLabel1);
        jPanel2.add(jScrollPane1);
        jScrollPane1.getViewport().add(jTable1);
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);
        jPanel1.add(jButton3);
        jPanel1.add(jButton1);
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
        jButton1.setText("删除类别");
    }
    private void initClass() {
        int total;
        try {
            ResultSet rs = Conn.query("select count(id) as total from 类别");
            rs.next();
            total  = rs.getInt("total");
            rs.close();
            rs = Conn.query("select * from 类别");
            this.dataModel1.data = new String[2][total];
            for (int i = 0; i<total; i++) {
                rs.next();
                dataModel1.data[0][i] = rs.getString("id");
                dataModel1.data[1][i] = rs.getString(2);
            }
            rs.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
        this.jTable1.addNotify();
        this.repaint();
        this.parent.initSelect();
    }

    public void jButton2_actionPerformed(ActionEvent e) {
        String s = Gload.getText(this.jTextField1);
        if (s == null || s.equals("")) {
            Gload.MSG(this, "请输入类别名称");
            return;
        }
        ResultSet rs = Conn.query("select id from 类别 where 类名='" + s + "'");
        try {
            if (rs.next()) {
                Gload.MSG(this, "相同的类名已经存在");
            } else {
                Conn.exec("insert into 类别(类名) values('" + s + "')");
                this.initClass();
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void jButton1_actionPerformed(ActionEvent e) {
        int i = this.jTable1.getSelectedRow();
        if (-1 == i) {
            Gload.MSG(this, "请选择一条记录");
            return;
        }
        String s = this.jTable1.getValueAt(i, 0).toString();
        if (s == null || s.equals("")) return;
        ResultSet rs = Conn.query("select id from 图书信息 where 类别ID=" + s);
        try {
            if (rs.next()) {
                rs.close();
                Gload.MSG(this, "存在属于该类的书,不能删除本类");
                return;
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        if (Gload.NO == Gload.Confirm(this, "确认删除吗?")) return;
        s = "delete from 类别 where id=" + s;
        Conn.exec(s);
        Gload.MSG(this, "删除成功");
        this.initClass();
    }

    public void jButton3_actionPerformed(ActionEvent e) {
        int i = this.jTable1.getSelectedRow();
        if (-1 == i) {
            Gload.MSG(this, "请选择一条记录");
            return;
        }
        String s = this.jTable1.getValueAt(i, 0).toString();
        if (s == null || s.equals("")) return;
        String s2 = JOptionPane.showInputDialog(this, "请输入新类名", "类别管理", 0);
        if (s2 == null || s2.equals("")) {
            return;
        }
        s2 = s2.replaceAll("'", "''");
        ResultSet rs = Conn.query("select id from 类别 where 类名='" + s2 + "'");
        try {
            if (rs.next()) {
                Gload.MSG(this, "相同的类名已经存在");
                rs.close();
                return;
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        Conn.exec("update 类别 set 类名='" + s2 + "' where id=" + s);
        Gload.MSG(this, "修改成功");
        this.initClass();
    }
}


class ClassMNG_jButton3_actionAdapter implements ActionListener {
    private ClassMNG adaptee;
    ClassMNG_jButton3_actionAdapter(ClassMNG adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton3_actionPerformed(e);
    }
}


class ClassMNG_jButton1_actionAdapter implements ActionListener {
    private ClassMNG adaptee;
    ClassMNG_jButton1_actionAdapter(ClassMNG adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}


class ClassMNG_jButton2_actionAdapter implements ActionListener {
    private ClassMNG adaptee;
    ClassMNG_jButton2_actionAdapter(ClassMNG adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class AbstractTableModel2 extends AbstractTableModel {
    private String[] title = {"自动编号", "类名"};
    public String[][] data;
    public AbstractTableModel2() {
        data = new String[title.length][1];
    }
    public int getColumnCount() { return title.length; }
    public int getRowCount() { return data[0].length;}
    public Object getValueAt(int row, int col) {
        if (data[col][row] == null)
            data[col][row] = "";
        return data[col][row];
    }
    public String getColumnName(int column){ return title[column];}//设置表格列名
}

⌨️ 快捷键说明

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