如何在swing 表格中实现对数据库的增删改查

mysql+swing

操作方法

  • 01

    创建mysql表student

  • 02

    创建student类

  • 03

    创建数据库连接及操作

  • 04

    创建事件监听

  • 05

    创建界面 package test1;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTable;import java.awt.Rectangle;import javax.swing.JScrollPane;import java.util.Vector;import java.sql.*;import java.util.ArrayList;import javax.swing.JLabel;import javax.swing.JTextField;import java.awt.Font;import javax.swing.JButton;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JOptionPane;public class JTableAppFrame extends JFrame {    JPanel contentPane;    Vector CellsVector = new Vector();    Vector TitleVector = new Vector();    JScrollPane scp = new JScrollPane();    JTable tab = null;    DBCon dbcon = new DBCon();    JLabel lbl_name = new JLabel();    JLabel lbl_age = new JLabel();    JLabel lbl_address = new JLabel();    JTextField txt_name = new JTextField();    JTextField txt_age = new JTextField();    JTextField txt_address = new JTextField();    JButton btn_add = new JButton();    JButton btn_del = new JButton();    JButton btn_update = new JButton();    JButton btn_reset = new JButton();    public JTableAppFrame() {        try {            setDefaultCloseOperation(EXIT_ON_CLOSE);            jbInit();        } catch (Exception exception) {            exception.printStackTrace();        }    }       private void jbInit() throws Exception {        contentPane = (JPanel) getContentPane();        contentPane.setLayout(null);        this.setResizable(false);        setSize(new Dimension(400, 340));        setTitle("JTable");        scp.setBounds(new Rectangle(46, 32, 297, 157));        lbl_name.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        lbl_name.setText("姓名:");        lbl_name.setBounds(new Rectangle(46, 205, 42, 15));        lbl_age.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        lbl_age.setText("年龄:");        lbl_age.setBounds(new Rectangle(200, 205, 42, 15));        lbl_address.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        lbl_address.setText("地址:");        lbl_address.setBounds(new Rectangle(46, 232, 42, 15));        txt_name.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        txt_name.setBounds(new Rectangle(98, 205, 72, 21));        txt_age.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        txt_age.setBounds(new Rectangle(250, 205, 72, 21));        txt_address.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        txt_address.setBounds(new Rectangle(98, 232, 72, 21));        btn_add.setBounds(new Rectangle(46, 271, 83, 25));        btn_add.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        btn_add.setText("添 加");        btn_add.addActionListener(new JTableAppFrame_btn_add_actionAdapter(this));        btn_del.setBounds(new Rectangle(155, 271, 83, 25));        btn_del.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        btn_del.setText("删 除");        btn_del.addActionListener(new JTableAppFrame_btn_del_actionAdapter(this));        btn_update.setBounds(new Rectangle(260, 271, 83, 25));        btn_update.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        btn_update.setText("修 改");        btn_update.addActionListener(new                                     JTableAppFrame_btn_update_actionAdapter(this));        btn_reset.setBounds(new Rectangle(258, 232, 83, 25));        btn_reset.setFont(new java.awt.Font("宋体", Font.BOLD, 12));        btn_reset.setText("重 置");        btn_reset.addActionListener(new JTableAppFrame_btn_reset_actionAdapter(this));        contentPane.add(scp);        contentPane.add(lbl_name);        contentPane.add(lbl_age);        contentPane.add(lbl_address);        contentPane.add(txt_name);        contentPane.add(txt_age);        contentPane.add(txt_address);        contentPane.add(btn_del);        contentPane.add(btn_update);        contentPane.add(btn_add);        contentPane.add(btn_reset);        dbcon.connectDB();        this.showTable();        tab = new JTable(CellsVector, TitleVector);        scp.getViewport().add(tab);    }       public void showTable() {        this.TitleVector.add("姓名");        this.TitleVector.add("年龄");        this.TitleVector.add("地址");        dbcon.select();        ArrayList list = dbcon.select();        for (int i = 0; i < list.size(); i++) {            Student stu = (Student) list.get(i);            Vector v = new Vector();            v.add(stu.getName());            v.add(stu.getAge());            v.add(stu.getAddress());            CellsVector.add(v);        }    }       String name;    String age;    String address;    public int checkInformation() {        name = this.txt_name.getText();        age = this.txt_age.getText();        address = this.txt_address.getText();        if (name.equals("")) {            JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", 1);            this.txt_name.grabFocus();            return 0;        }        if (age.equals("")) {            JOptionPane.showMessageDialog(this, "您好!请输入年龄!", "提示", 1);            this.txt_age.grabFocus();            return 0;        }        char[] ans = age.toCharArray();        for (int i = 0; i < ans.length; i++) {            if (!Character.isDigit(ans[i])) {                JOptionPane.showMessageDialog(this, "您好!年龄输入错误!", "提示", 1);                this.txt_age.setText("");                this.txt_age.grabFocus();                return 0;            }        }        if (age.length() > 3) {            JOptionPane.showMessageDialog(this, "您好!年龄最高只能为 100 岁!", "提示", 1);            this.txt_age.setText("");            this.txt_age.grabFocus();            return 0;        }        if (address.equals("")) {            JOptionPane.showMessageDialog(this, "您好!请输入地址!", "提示", 1);            this.txt_address.grabFocus();            return 0;        }        ages = Integer.valueOf(age);        return 1;    }       int ages;    public void btn_add_actionPerformed(ActionEvent e) {        if (this.checkInformation() == 0) {            return;        }        dbcon.insert(name, ages, address);        this.CellsVector.clear();        this.showTable();        this.tab.updateUI();        this.btn_reset_actionPerformed(e);    }       ResultSet rs;    public void btn_del_actionPerformed(ActionEvent e) {        String name = this.txt_name.getText();        this.txt_age.setEditable(false);        this.txt_address.setEditable(false);        if (name.equals("")) {            JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", 1);            this.txt_name.grabFocus();            return;        }        try {            String sql = "select * from Student where username=?";            PreparedStatement ps = DBCon.con.prepareStatement(sql);            ps.setString(1, name);            rs = ps.executeQuery();            if (rs.next()) {                ps.close();                dbcon.delete(name);                this.CellsVector.clear();                this.showTable();                this.tab.updateUI();                this.btn_reset_actionPerformed(e);            } else {                JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", 1);                this.btn_reset_actionPerformed(e);                return;            }        } catch (SQLException ex) {            System.out.println("核对学员信息发生异常" + ex.getMessage());        }    }       public void btn_update_actionPerformed(ActionEvent e) {        if (this.checkInformation() == 0) {            return;        }        try {            String sql = "select * from Student where username=?";            PreparedStatement ps = DBCon.con.prepareStatement(sql);            ps.setString(1, name);            rs = ps.executeQuery();            if (rs.next()) {                ps.close();                dbcon.update(name, ages, address);                this.CellsVector.clear();                this.showTable();                this.tab.updateUI();                this.btn_reset_actionPerformed(e);            } else {                JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", 1);                this.btn_reset_actionPerformed(e);                return;            }        } catch (SQLException ex) {            System.out.println("核对学员信息发生异常" + ex.getMessage());        }    }       public void btn_reset_actionPerformed(ActionEvent e) {        this.txt_name.setText("");        this.txt_age.setText("");        this.txt_address.setText("");        this.txt_age.setEditable(true);        this.txt_address.setEditable(true);    }}

  • 06

    创建调用

  • 07

    成功后

(0)

相关推荐

  • SQL数据库常用方法增删改查

    SQL数据库常用方法增删改查以及其它方法使用 操作方法 01 SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 SQL 可更新数据库中的数据 ...

  • 如何在excel表格中添加斜线

    在我们日常数据的计算中Excel确实给我带来非常多的方便快捷,但有时制作些较复杂的表格就相形见拙了,就不如word那样应用自如了,例如:在一个单元格中要制作e画斜线就没word那样随心所欲了,所见所得 ...

  • 如何在wps表格中同时冻结行和列?怎样取消冻结

    在我们生活使用WPS表格的时候,由于表格的列和行太多,在一屏里不能呈现完,所以我们需要在wps表格中同时冻结行和列,下面就让小编告诉你 如何在wps表格中同时冻结行和列的方法. 在wps表格中同时冻结 ...

  • 如何在wps表格中使用千位分隔符?

    wps的表格是很好用的软件,那么如何在wps表格中使用千位分隔符?今天就来教给大家吧~具体如下:1. 首先打开wps表格.2. 随后随意想表格中输入数字.3. 选中刚刚输入数字的表格后,右键.4. 随 ...

  • 如何在WPS表格中锁定标题冻结不动

    WPS的使用在当代社会非常流行,但是我们在使用WPS时会出现各种各样的问题.那么小编就在这儿为大家解决如何在WPS表格中锁定标题冻结不动这一问题吧!具体如下:1.第一步,打开表格.2. 第二步,如图所 ...

  • 如何在Excel表格中设置双面打印

    Excel是我们常用的一款数据处理软件,深受大家的欢迎,是我们学习办公的助手,有的朋友想知道怎么如何在Excel表格中设置双面打印,小编为大家解答这个问题.具体如下:1. 首先,请大家在自己的电脑中找 ...

  • 如何在wps表格中更改图表坐标轴

    有的小伙伴在使用WPS表格软件处理数据时,想要更改图表坐标轴,但是却不知道如何更改,那么小编就来为大家介绍一下吧.具体如下:1.  第一步,双击或者右击打开WPS表格,在下图所示的主页面中,输入并选中 ...

  • 如何在WPS表格中互换转置行和列

    有的小伙伴在使用WPS表格软件时,一不小心输入错了行和列,但是又不想重新输入表格,那么如何快速互换转置行和列呢?小编就来为大家介绍一下吧.具体如下:1. 第一步,双击或者右击打开WPS表格,接着在每行 ...

  • 如何在wps表格中批量删除空白行

    有的小伙伴在使用WPS表格软件处理数据时,发现表格中有很多空白行,如果一行一行删除,那么肯定会非常麻烦,如何批量删除空白行呢?小编就来为大家介绍一下吧.具体如下:1. 第一步,双击或者右击打开需要删除 ...