/*
* Created by JFormDesigner on Sat Jun 01 17:00:13 CST 2024
*/
package Swing;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
/**
* @author qwerty
*/
public class Manager extends JFrame {
public Manager() {
initComponents();
}
private void button1(ActionEvent e) {
//添加
Integer id = Integer.parseInt(学号1.getText());
String name = 姓名1.getText();
Double math = Double.parseDouble(数学1.getText().trim());
Double phy = Double.parseDouble(物理1.getText().trim());
Double eng = Double.parseDouble(英语1.getText().trim());
Stu stu = new Stu(id,name,math,phy,eng);
try {
addStu(stu);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
System.out.println(stu);
} //添加
public void addStu(Stu stu) throws ClassNotFoundException, SQLException {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://127.0.0.1:3308/filesys"; // jdbc:mysql:// ip: + 端口号/ + 被操作数据库的名字
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//定义查询语句和执行sql语句
//String sql = "SELECT * FROM test";
String sql = "insert into test (id,name,math,phy,english) values (?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,stu.getId());
ps.setString(2,stu.getName());
ps.setDouble(3,stu.getMath());
ps.setDouble(4,stu.getPhysical());
ps.setDouble(5,stu.getEnglish());
int i = ps.executeUpdate();
System.out.println("执行成功!修改了"+i+"条语句");
System.out.println("处理结果集成功");
//释放资源
ps.close();
conn.close();
} //添加的SQL语句
private void button2(ActionEvent e) {
//查询
try {
bianli();
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} //查询
public void bianli() throws ClassNotFoundException, SQLException {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
String url = "jdbc:mysql://127.0.0.1:3308/filesys"; // jdbc:mysql:// ip: + 端口号/ + 被操作数据库的名字
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//定义查询语句和执行sql语句
//String sql = "SELECT * FROM test";
String sql = "select * from test";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){//循环取数据
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
Double math = resultSet.getDouble("math");
Double phy = resultSet.getDouble("phy");
Double en = resultSet.getDouble("english");
System.out.println("编号:"+id+"\t名字:"+name+"\t数学成绩:"+math+"\t物理成绩:"+phy+"\t英语成绩:" + en);
}
System.out.println("处理结果集成功");
//释放资源
ps.close();
conn.close();
} //查询的SQL语句(需要补充)
private void button4(ActionEvent e) {
//修改
Integer id = Integer.parseInt(学号2.getText());
String name = 姓名2.getText();
Double math = Double.parseDouble(数学2.getText().trim());
Double phy = Double.parseDouble(物理2.getText().trim());
Double eng = Double.parseDouble(英语2.getText().trim());
Integer id2 = Integer.parseInt(修改.getText().trim());
Stu stu = new Stu(id,name,math,phy,eng);
try {
upda(stu,id2);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
} {
}
} //修改
public void upda(Stu stu,int id2) throws ClassNotFoundException, SQLException {
//获取连接
String url = "jdbc:mysql://127.0.0.1:3308/filesys"; // jdbc:mysql:// ip: + 端口号/ + 被操作数据库的名字
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//定义查询语句和执行sql语句
String sql = "update test SET id= ?,name=?,math = ?,phy=?,english=? where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,stu.getId());
ps.setString(2,stu.getName());
ps.setDouble(3,stu.getMath());
ps.setDouble(4,stu.getPhysical());
ps.setDouble(5,stu.getEnglish());
ps.setInt(6,id2);
int i = ps.executeUpdate();
System.out.println("执行成功!修改了"+i+"条语句");
System.out.println("处理结果集成功");
//释放资源
ps.close();
conn.close();
} //修改的SQL语句
private void button3(ActionEvent e) {
int id3 = Integer.parseInt(textField7.getText());
//删除
try {
delet(id3);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} //删除
public void delet(int id3) throws ClassNotFoundException, SQLException {
//获取连接
String url = "jdbc:mysql://127.0.0.1:3308/filesys"; // jdbc:mysql:// ip: + 端口号/ + 被操作数据库的名字
String username = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, username, password);
//定义查询语句和执行sql语句
//String sql = "SELECT * FROM test";
String sql = "delete from test where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id3);
int i = ps.executeUpdate();
System.out.println("执行成功!修改了"+i+"条语句");
System.out.println("处理结果集成功");
//释放资源
ps.close();
conn.close();
} //删除的SQL语句
private void initComponents() {
tabbedPane1 = new JTabbedPane();
panel4 = new JPanel();
label4 = new JLabel();
label5 = new JLabel();
label6 = new JLabel();
label7 = new JLabel();
label8 = new JLabel();
学号1 = new JTextField();
姓名1 = new JTextField();
数学1 = new JTextField();
英语1 = new JTextField();
物理1 = new JTextField();
button1 = new JButton();
panel5 = new JPanel();
scrollPane1 = new JScrollPane();
table1 = new JTable();
button2 = new JButton();
panel6 = new JPanel();
label9 = new JLabel();
修改 = new JTextField();
separator1 = new JSeparator();
label16 = new JLabel();
label17 = new JLabel();
label18 = new JLabel();
label19 = new JLabel();
label20 = new JLabel();
学号2 = new JTextField();
数学2 = new JTextField();
姓名2 = new JTextField();
物理2 = new JTextField();
英语2 = new JTextField();
button4 = new JButton();
panel7 = new JPanel();
label10 = new JLabel();
label12 = new JLabel();
label13 = new JLabel();
label1