JOBC编程题
设计一个最基本界面实现对数据库表格中数据的增删查改
package JOBCTEST;
import java.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
public class TESTFRAME extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JLabel lblName;
private JLabel lblAge;
private JTextField textField_3;
private JButton btnDelete;
private JButton btnInsert;
private JTextField textField_4;
private JTextField textField_5;
private JLabel label;
private JLabel lblName_1;
private JLabel label_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TESTFRAME frame = new TESTFRAME();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TESTFRAME() {
setTitle("MESSAGE");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 636, 420);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblId = new JLabel("ID");
lblId.setFont(new Font("宋体", Font.PLAIN, 23));
lblId.setBounds(61, 29, 45, 27);
contentPane.add(lblId);
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setFont(new Font("宋体", Font.BOLD, 21));
textField.setBounds(131, 30, 146, 27);
contentPane.add(textField);
textField.setColumns(10);
JButton btnQuery = new JButton("Query");
btnQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String id=textField.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=true&characterEncoding=utf-8","root","");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from Student where ID="+id);
if(rs.next()==false)
textField_1.setText("-----NONE-----");
else
{
String s=rs.getString(1)+"\n"+rs.getString(3)+"\n"+rs.getString(1)+"\n";
textField_1.setText(rs.getString(1));
textField_4.setText(rs.getString(2));
textField_5.setText(rs.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
btnQuery.setBounds(391, 30, 123, 29);
contentPane.add(btnQuery);
textField_1 = new JTextField();
textField_1.setHorizontalAlignment(SwingConstants.CENTER);
textField_1.setFont(new Font("宋体", Font.BOLD, 21));
textField_1.setBounds(23, 279, 165, 42);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setHorizontalAlignment(SwingConstants.CENTER);
textField_2.setFont(new Font("宋体", Font.BOLD, 21));
textField_2.setColumns(10);
textField_2.setBounds(130, 87, 146, 29);
contentPane.add(textField_2);
lblName = new JLabel("Name");
lblName.setFont(new Font("宋体", Font.PLAIN, 23));
lblName.setBounds(61, 83, 66, 27);
contentPane.add(lblName);
lblAge = new JLabel("Age");
lblAge.setFont(new Font("宋体", Font.PLAIN, 23));
lblAge.setBounds(61, 148, 66, 27);
contentPane.add(lblAge);
textField_3 = new JTextField();
textField_3.setHorizontalAlignment(SwingConstants.CENTER);
textField_3.setFont(new Font("宋体", Font.BOLD, 21));
textField_3.setColumns(10);
textField_3.setBounds(131, 149, 146, 29);
contentPane.add(textField_3);
btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String id=textField.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=true&characterEncoding=utf-8","root","");
Statement sql=con.createStatement();
sql.executeUpdate("delete from Student where ID="+id);
textField_1.setText("Has deleted");
textField_4.setText("Has deleted");
textField_5.setText("Has deleted");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
});
btnDelete.setBounds(391, 173, 123, 29);
contentPane.add(btnDelete);
btnInsert = new JButton("Insert");
btnInsert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String id=textField.getText();
String name=textField_2.getText();
int age=Integer.parseInt(textField_3.getText());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useSSL=true&characterEncoding=utf-8","root","");
Statement sql=con.createStatement();
String s="insert into Student values('" +id+"','"+name+"',"+age+")";
sql.executeUpdate(s);
textField_1.setText("Has added");
textField_4.setText("Has added");
textField_5.setText("Has added");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
});
btnInsert.setBounds(391, 103, 123, 29);
contentPane.add(btnInsert);
textField_4 = new JTextField();
textField_4.setHorizontalAlignment(SwingConstants.CENTER);
textField_4.setFont(new Font("宋体", Font.BOLD, 21));
textField_4.setColumns(10);
textField_4.setBounds(225, 279, 158, 42);
contentPane.add(textField_4);
textField_5 = new JTextField();
textField_5.setHorizontalAlignment(SwingConstants.CENTER);
textField_5.setFont(new Font("宋体", Font.BOLD, 21));
textField_5.setColumns(10);
textField_5.setBounds(414, 279, 158, 42);
contentPane.add(textField_5);
label = new JLabel("ID");
label.setFont(new Font("宋体", Font.PLAIN, 23));
label.setBounds(82, 237, 45, 27);
contentPane.add(label);
lblName_1 = new JLabel("Name");
lblName_1.setFont(new Font("宋体", Font.PLAIN, 23));
lblName_1.setBounds(270, 237, 66, 27);
contentPane.add(lblName_1);
label_1 = new JLabel("Age");
label_1.setFont(new Font("宋体", Font.PLAIN, 23));
label_1.setBounds(472, 237, 66, 27);
contentPane.add(label_1);
}
}
在这里插入代码片