java增删改查 连接数据库

2021/12/29 19:08:00

本文主要是介绍java增删改查 连接数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

java增删改查 连接数据库

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;



public class 乘客管理 extends JPanel {

    JButton chaxunJButton =new JButton("查询");
    JButton xiugaiJButton =new JButton("修改");
    JButton zengjiaJButton =new JButton("添加乘客");
    JButton liulanJButton =new JButton("浏览");
    JButton shanchuJButton =new JButton("注销");
    public 乘客管理()
    {
        add(chaxunJButton);
        add(xiugaiJButton);
        add(zengjiaJButton);
        add(liulanJButton);
        add(shanchuJButton);
        setVisible(true);

        chaxunJButton.addActionListener(new chaxunListen());
        zengjiaJButton.addActionListener(new zengjiaListen());
        xiugaiJButton.addActionListener(new xiugaiListen());
        liulanJButton.addActionListener(new liulanListen());
        shanchuJButton.addActionListener(new shanchuListen());
    }

    //查询乘客按钮实现打开查询乘客页面的功能
    class chaxunListen implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            new 乘客查询();
        }
    }

    //增加乘客按钮实现打开增加乘客页面的功能
    class zengjiaListen implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            new 乘客增加();
        }

    }

    //修改乘客按钮实现打开修改乘客页面的功能
    class xiugaiListen implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            new 乘客修改();
        }
    }

    //浏览所有乘客按钮实现打开浏览所有乘客的功能
    class liulanListen implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            new 乘客浏览();
        }
    }
    class shanchuListen implements ActionListener
    {
        public void actionPerformed(ActionEvent e){
            new 删除乘客();
        }
    }


    public static class 乘客浏览 extends JFrame
    {
        Object data[][];
        Object colname[]={"名字","用户名","密码","身份证号"};

        private JTable ckxinxiTable;

        JButton ok = new JButton("查询所有乘客");
        public 乘客浏览()
        {
            setLayout(new GridLayout(4, 1, 0, 10));
            add(ok);

            setTitle("浏览所有乘客");
            setLocation(200,200);
            setSize(500, 300);
            setVisible(true);
            ok.addActionListener(new okListen());
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }

        class okListen implements ActionListener
        {


            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");


                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器

                    String sql = "select * from chengke";
                    System.out.println(sql);
                    ResultSet rs = stmt.executeQuery(sql);
                    //executeQuery执行查询语句


                    //*查询结果集的并显示在表格里
                    rs.last();
                    int n=rs.getRow();

                    data = new Object[n][10];
                    ckxinxiTable = new JTable(data, colname);
                    add(new JScrollPane(ckxinxiTable));
                    setVisible(true);
                    rs.beforeFirst();
                    int i=0;
                    while( rs.next()) {
                        data[i][0] = rs.getString(1);
                        data[i][1] = rs.getString(2);
                        data[i][2] = rs.getString(3);
                        data[i][3] = rs.getString(4);
                        i++;
                    }

                    //关闭数据库
                    rs.close();
                    stmt.close();
                    conn.close();

                }
                catch( Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }
        }


        public static void main(String[] args)
        {
            new 乘客浏览();
        }
    }

    public static class 乘客查询 extends JFrame
    {
        Object data[][];
        Object colname[]={"名字","用户名","密码","身份证号"};
        private JTable ckTable;

        JTextField name = new JTextField(10);
        JButton ok = new JButton("确定");
        public 乘客查询()
        {
            setLayout(new GridLayout(4, 1, 0, 10));

            add(new JLabel("请输入需要查询的乘客名字"));
            add(name);
            add(ok);

            setTitle("查询乘客");
            setLocation(200,200);
            setSize(500, 500);
            setVisible(true);
            ok.addActionListener(new okListen());
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }

        class okListen implements ActionListener
        {


            public void actionPerformed(ActionEvent e) {
                System.out.println(name.getText());
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动
                    // MySQL的JDBC驱动(8.0版本) 驱动名称改变为:com.mysql.cj.jdbc.Driver

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");


                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器

                    String sql = "select * from chengke where mingzi='"+ name.getText()+"'";
                    System.out.println(sql);
                    ResultSet rs = stmt.executeQuery(sql);
                    //executeQuery执行查询语句


                    //*查询结果集的并显示在表格里
                    rs.last();
                    int n=rs.getRow();

                    data = new Object[n][10];
                    ckTable = new JTable(data, colname);
                    add(new JScrollPane(ckTable));
                    setVisible(true);
                    rs.beforeFirst();
                    int i=0;
                    while( rs.next()) {
                        data[i][0] = rs.getString(1);
                        data[i][1] = rs.getString(2);
                        data[i][2] = rs.getString(3);
                        data[i][3] = rs.getString(4);
                        i++;
                    }

                    //关闭数据库
                    rs.close();
                    stmt.close();
                    conn.close();

                }
                catch( Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }
        }


        public static void main(String[] args)
        {
            new 乘客查询();
        }
    }

    public static class 乘客增加 extends JFrame {


        JTextField mingziJTextField = new JTextField(10);
        JTextField nameJTextField = new JTextField(10);
        JTextField passwordJTextField = new JTextField(10);
        JTextField idJTextField = new JTextField(10);
        JButton jieshuJButton = new JButton("结束");

        public 乘客增加() {

            setLayout(new GridLayout(5, 2, 0, 10));

            add(new JLabel("请输入增加乘客的姓名"));
            add(mingziJTextField);
            add(new JLabel("请输入增加乘客的用户名"));
            add(nameJTextField);
            add(new JLabel("请输入增加乘客的密码"));
            add(passwordJTextField);
            add(new JLabel("请输入增加乘客的身份证号"));
            add(idJTextField);
            add(jieshuJButton);

            setTitle("增加信息");
            setLocation(200, 200);
            setSize(600, 400);
            setVisible(true);
            jieshuJButton.addActionListener(new jieshuJButtonListen());
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }

        class jieshuJButtonListen implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");
                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器
                    String sql = "insert into chengke(mingzi,name,password,id)" +
                            "values('"+ mingziJTextField.getText()+"','"+ nameJTextField.getText()+"','"+ passwordJTextField.getText()+"','"+ idJTextField.getText()+"')";
                    System.out.println(sql);
                    int rs = stmt.executeUpdate(sql);
                    //executeQuery执行查询语句;stmt.executeUpdate(sql)执行增删改语句
                    JOptionPane.showMessageDialog(null, "添加成功");
                } catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }


        }


        public static void main(String[] args)
        {
            new 乘客增加();
        }



    }

    public static class 乘客修改 extends JFrame{

        JTextField mingziJTextField = new JTextField(10);
        JTextField xiugaixinxiJTextField = new JTextField(10);
        JButton genggaimingziJButton = new JButton("更改名字");
        JButton genggaiyonghumingJButton = new JButton("更改用户名");
        JButton genggaimimaJButton = new JButton("更改密码");

        public 乘客修改(){
            setLayout(new GridLayout(9, 1, 0, 10));

            add(new JLabel("请输入需要修改的乘客名字"));
            add(mingziJTextField);
            add(new JLabel("请先输入修改后的信息再点击要修改的相应信息按钮"));
            add(xiugaixinxiJTextField);
            add(genggaimingziJButton);
            add(genggaiyonghumingJButton);
            add(genggaimimaJButton);

            setTitle("修改乘客信息");
            setLocation(200, 200);
            setSize(500, 500);
            setVisible(true);
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            genggaimingziJButton.addActionListener(new xiangxixinxiJButtonListen());
            genggaiyonghumingJButton.addActionListener(new nameJButtonListen());
            genggaimimaJButton.addActionListener(new chengkeJButtonListen());

        }
        //更改详细信息按钮触发的修改详细信息事件
        class xiangxixinxiJButtonListen implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                // System.out.println(name.getText());
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动
                    // MySQL的JDBC驱动(8.0版本) 驱动名称改变为:com.mysql.cj.jdbc.Driver

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");


                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器


                    String sql = "update chengke " +
                            "set mingzi = '"+ xiugaixinxiJTextField.getText()+"'" +
                            "where mingzi = '"+ mingziJTextField.getText() +"'";
                    System.out.println(sql);
                    int rs = stmt.executeUpdate(sql);
                    JOptionPane.showMessageDialog(genggaimingziJButton, "更改成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                }
                catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }

        }


        //修改用户名按钮触发的修改用户名事件
        class nameJButtonListen implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                // System.out.println(name.getText());
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动
                    // MySQL的JDBC驱动(8.0版本) 驱动名称改变为:com.mysql.cj.jdbc.Driver

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");


                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器


                    String sql = "update chengke " +
                            "set name = '"+ xiugaixinxiJTextField.getText()+"'" +
                            "where mingzi = '"+ mingziJTextField.getText() +"'";
                    System.out.println(sql);
                    int rs = stmt.executeUpdate(sql);
                    JOptionPane.showMessageDialog(genggaiyonghumingJButton, "更改成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    //stmt.executeUpdate(sql)执行增删改语句


                } catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }

        }

        //修改乘客按钮触发的修改乘客事件
        class chengkeJButtonListen implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                // System.out.println(name.getText());
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动


                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");

                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器


                    String sql = "update chengke " +
                            "set password = '"+ xiugaixinxiJTextField.getText()+"'" +
                            "where mingzi = '"+ mingziJTextField.getText() +"'";
                    System.out.println(sql);
                    int rs = stmt.executeUpdate(sql);
                    JOptionPane.showMessageDialog(genggaimimaJButton, "更改成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
                    //stmt.executeUpdate(sql)执行增删改语句


                } catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }

        }

        public static void main(String[] args)
        {
            new 乘客修改();
        }

    }

    public static class 删除乘客 extends JFrame {


        JTextField zhanghuJTextField = new JTextField(10);
        JButton quedingJButton = new JButton("确定");

        public 删除乘客() {

            setLayout(new GridLayout(3, 1, 0, 10));

            add(new JLabel("请输入需要删除的乘客名"));
            add(zhanghuJTextField);
            add(quedingJButton);

            setTitle("删除账号");
            setLocation(200, 200);
            setSize(500, 300);
            setVisible(true);
            quedingJButton.addActionListener(new quedingJButtonListen());
            setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        }

        class quedingJButtonListen implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                // System.out.println(name.getText());
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");//第一步:加载jdbc驱动
                    // MySQL的JDBC驱动(8.0版本) 驱动名称改变为:com.mysql.cj.jdbc.Driver

                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/mysql", "root", "1234");
                    //第二步,根据情况进行更改:连接数据库school,用户名:root,密码:root

                    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                    //第三步:生成容器

                    // String sql = "select * from 患者信息 where 药品名字='"+ name.getText()+"'";
                    String sql = "delete from chengke " +
                            "where name = '"+zhanghuJTextField.getText()+"'";
                    System.out.println(sql);
                    int rs = stmt.executeUpdate(sql);
                    //executeQuery执行查询语句;stmt.executeUpdate(sql)执行增删改语句
                    JOptionPane.showMessageDialog(quedingJButton, "删除成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);

                } catch (Exception e1) {
                    System.out.println(e1.getMessage());
                }

            }


        }


        public static void main(String[] args)
        {
            new 删除乘客();
        }



    }
}




这篇关于java增删改查 连接数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程