java實現簡單的JDBC

Tibty.發表於2020-12-13

JDBC類:用於實現swing介面和啟動執行緒

package unit_five;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JDBC implements ActionListener {
    JFrame jf;
    JPanel jp1,jp2;
    JLabel l;
    JButton add,del,up,show;

    JDBC(){
        jf=new JFrame("JDBC");
        jp1=new JPanel();
        jp2=new JPanel();
        l=new JLabel("請選擇功能^-^");
        jp1.add(l);
        add=new JButton("增加");
        add.addActionListener(this);
        del=new JButton("刪除");
        del.addActionListener(this);
        up=new JButton("更新");
        up.addActionListener(this);
        show=new JButton("查詢");
        show.addActionListener(this);
        jp2.setLayout(null);
        add.setBounds(100,10,70,40);
        add.setFont(new Font("宋體",Font.BOLD,17));
        add.setBackground(Color.orange);
        del.setBounds(100,70,70,40);
        del.setFont(new Font("宋體",Font.BOLD,17));
        del.setBackground(Color.orange);
        up.setBounds(100,130,70,40);
        up.setFont(new Font("宋體",Font.BOLD,17));
        up.setBackground(Color.orange);
        show.setBounds(100,190,70,40);
        show.setFont(new Font("宋體",Font.BOLD,17));
        show.setBackground(Color.orange);

        jp2.add(add);
        jp2.add(del);
        jp2.add(up);
        jp2.add(show);
        jf.setBounds(200,100,300,300);
        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });

        jf.setLayout(new BorderLayout());
        jp1.setBackground(Color.pink);
        jp2.setBackground(Color.pink);
        jf.add(jp1,BorderLayout.NORTH);
        jf.add(jp2,BorderLayout.CENTER);
        jf.setVisible(true);
    }


    public static void main(String[] args) {
        new JDBC();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s=e.getActionCommand();
        if(s.equals("增加")){
            function.add a=new function().new add();
            Thread t=new Thread(a);
            t.start();
        }else if(s.equals("刪除")){
            function.del d=new function().new del();
            Thread t=new Thread(d);
            t.start();
        }else if(s.equals("更新")){
            function.change c=new function().new change();
            Thread t=new Thread(c);
            t.start();

        }else{
            function.showall showAll=new function().new showall();
            Thread t=new Thread(showAll);
            t.start();
        }
    }
}

function類:用於實現具體功能

package unit_five;

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

public class function {
    static String url="jdbc:mysql://localhost:3306/test?useSSL=false";//test是自己在mysql資料庫裡面建立的資料庫的名字
    static String root="root";
    static String pwd="12345";//mysql中root的密碼
    static {
        try{
            Class.forName("com.mysql.jdbc.Driver");//驅動
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private static Connection getConn(){
        Connection con=null;
        try {
            con= DriverManager.getConnection(url,root,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    //查詢功能類
    class showall implements Runnable{
        JPanel jp=new JPanel();
        JFrame jf =new JFrame("查詢");
        @Override
        public void run() {
            jf.setBounds(200,100,600,600);
            Connection con = getConn();
            try {
                PreparedStatement pst = con.prepareStatement("select * from User");
                ResultSet rs = pst.executeQuery();

                String[] columnNames = {"id", "loginName", "password", "gender", "name", "age"};// 定義表格列
                String[][] tableValues = new String[30][columnNames.length];// 定義陣列,用來儲存表格資料

                for (int row = 0; row < tableValues.length; row++) {
                    if (rs.next()) {
                        for (int column = 0; column < columnNames.length; column++) {
                            if (column == 0 || column == 5) {
                                tableValues[row][column] = String.valueOf(rs.getInt(column + 1));// 給表格賦值
                            } else {
                                tableValues[row][column] = rs.getString(column + 1);// 給表格賦值
                            }
                        }
                    }
                }

                JTable table = new JTable(tableValues, columnNames);
                JScrollPane sc = new JScrollPane(table);
                jp.add(sc);
                jf.add(jp);
                jf.setVisible(true);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //刪除功能類
    class del implements Runnable{
        JFrame jf=new JFrame("刪除");
        JPanel jp=new JPanel();
        JLabel l1=new JLabel("id");
        JButton yes=new JButton("確定");
        JTextField jtf=new JTextField(10);

        @Override
        public void run() {
            jf.setBounds(100,100,500,200);
            jp.setBackground(Color.pink);
            jp.add(l1);
            jp.add(jtf);
            jp.add(yes);
            yes.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Connection con = getConn();
                    try {
                        PreparedStatement pst = con.prepareStatement("delete from User where id=?");
                        pst.setInt(1,Integer.parseInt(jtf.getText()));
                        pst.executeUpdate();

                    } catch (SQLException e2) {
                        e2.printStackTrace();
                    }
                }
            });
            jf.add(jp);
            jf.setVisible(true);

        }
    }
    //插入功能類
    class add implements Runnable{
        JFrame jf=new JFrame("新增");
        JPanel jp=new JPanel();
        JLabel l1=new JLabel("id");
        JLabel l2=new JLabel("loginName");
        JLabel l3=new JLabel("password");
        JLabel l4=new JLabel("name");
        JLabel l5=new JLabel("gender");
        JLabel l6=new JLabel("age");
        JButton yes=new JButton("確定");
        JTextField jtf1=new JTextField(10);
        JTextField jtf2=new JTextField(15);
        JTextField jtf3=new JTextField(10);
        JTextField jtf4=new JTextField(12);
        JTextField jtf5=new JTextField(10);
        JTextField jtf6=new JTextField(10);
        @Override
        public void run() {
            jf.setBounds(100,100,500,200);
            jp.setBackground(Color.pink);
            jp.add(l1);
            jp.add(jtf1);
            jp.add(l2);
            jp.add(jtf2);
            jp.add(l3);
            jp.add(jtf3);
            jp.add(l4);
            jp.add(jtf4);
            jp.add(l5);
            jp.add(jtf5);
            jp.add(l6);
            jp.add(jtf6);
            jp.add(yes);
            yes.addActionListener(e -> {
                Connection con = getConn();
                try {
                    PreparedStatement pst = con.prepareStatement("insert into "+"User(id,loginName,password,name,gender,age)"+" values(?,?,?,?,?,?)");
                    pst.setInt(1,Integer.parseInt(jtf1.getText()));
                    pst.setString(2,jtf2.getText());
                    pst.setString(3,jtf3.getText());
                    pst.setString(4,jtf4.getText());
                    pst.setString(5,jtf5.getText());
                    pst.setInt(6,Integer.parseInt(jtf6.getText()));
                    pst.executeUpdate();

                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
            });
            jf.add(jp);
            jf.setVisible(true);

        }
    }
    //更新功能類
    class change implements Runnable{
        JFrame jf=new JFrame("更新");
        JPanel jp=new JPanel();
        JLabel l1=new JLabel("password");
        JLabel l2=new JLabel("age");
        JLabel l3=new JLabel("gender");
        JLabel l4=new JLabel("name");
        JLabel l5=new JLabel("id");
        JButton yes=new JButton("確定");
        JTextField jtf1=new JTextField(15);
        JTextField jtf2=new JTextField(15);
        JTextField jtf3=new JTextField(10);
        JTextField jtf4=new JTextField(12);
        JTextField jtf5=new JTextField(10);
        @Override
        public void run() {
            jf.setBounds(100,100,500,200);
            jp.setBackground(Color.pink);
            jp.add(l1);
            jp.add(jtf1);
            jp.add(l2);
            jp.add(jtf2);
            jp.add(l3);
            jp.add(jtf3);
            jp.add(l4);
            jp.add(jtf4);
            jp.add(l5);
            jp.add(jtf5);
            jp.add(yes);
            yes.addActionListener(e -> {
                Connection con = getConn();
                try {
                    PreparedStatement pst = con.prepareStatement("update user set"+" password=?,age=?,gender=?,name=? where id=?");
                    pst.setString(1,jtf1.getText());
                    pst.setInt(2,Integer.parseInt(jtf2.getText()));
                    pst.setString(3,jtf3.getText());
                    pst.setString(4,jtf4.getText());
                    pst.setInt(5,Integer.parseInt(jtf5.getText()));
                    pst.executeUpdate();

                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
            });
            jf.add(jp);
            jf.setVisible(true);

        }
    }

}

最終效果(趕時間隨便寫的一個,介面可能不太美觀):
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

相關文章