1. 自选数据库管理系统创建数据库stu,按照下表的结构创建"student"表: 字段名 Java 数据类型 宽度 SQL 数据类型 id int 10 int Name String 20 Char(20) Sex String 2 Char(2) Age Int 3 Integer id Name Sex Age 1 张小明 男 18 2 李雷 男 19 3 韩梅梅 女 18 4. 将数据表 stu 及其数据操作封装成类, 将数据操作功能封装成类的方法, 通过该类, 借助图形用户界面实现下面功能: (1) 向表中增加记录并显示增加后的所有记录(新增记录的具体数据自定); (2) 从表中删除 id=1 的记录, 并显示删除后的所有记录; (3) 修改表中记录: 查询条件 id=2,将 name 修改为: 王杰,修改完毕显示所有记录; (4) 查询表中 id=3 的记录并显示。编写该代码
时间: 2024-03-02 18:50:17 浏览: 75
下面是一个示例代码,实现了上述要求:
Student.java:
```java
public class Student {
private int id;
private String name;
private String sex;
private int age;
public Student(int id, String name, String sex, int age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
// getter and setter methods
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String toString() {
return "id: " + id + ", name: " + name + ", sex: " + sex + ", age: " + age;
}
}
```
StudentDAO.java:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private String url;
private String username;
private String password;
public StudentDAO(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
public void add(Student student) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO student (id, name, sex, age) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, student.getId());
stmt.setString(2, student.getName());
stmt.setString(3, student.getSex());
stmt.setInt(4, student.getAge());
stmt.executeUpdate();
} finally {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
}
public void delete(int id) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROM student WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} finally {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
}
public void update(int id, String name) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "UPDATE student SET name = ? WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setInt(2, id);
stmt.executeUpdate();
} finally {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
}
public Student findById(int id) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM student WHERE id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
return new Student(rs.getInt("id"), rs.getString("name"), rs.getString("sex"), rs.getInt("age"));
}
return null;
} finally {
if (rs != null) { rs.close(); }
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
}
public List<Student> findAll() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM student";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
List<Student> students = new ArrayList<>();
while (rs.next()) {
students.add(new Student(rs.getInt("id"), rs.getString("name"), rs.getString("sex"), rs.getInt("age")));
}
return students;
} finally {
if (rs != null) { rs.close(); }
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
}
}
```
Main.java:
```java
import java.sql.SQLException;
import java.util.List;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/stu?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = "123456";
StudentDAO dao = new StudentDAO(url, username, password);
try {
// add a new student
Student newStudent = new Student(5, "刘德华", "男", 21);
dao.add(newStudent);
List<Student> students = dao.findAll();
System.out.println("After adding:");
for (Student student : students) {
System.out.println(student);
}
// delete a student by id
dao.delete(1);
students = dao.findAll();
System.out.println("After deleting:");
for (Student student : students) {
System.out.println(student);
}
// update a student's name by id
dao.update(2, "王杰");
students = dao.findAll();
System.out.println("After updating:");
for (Student student : students) {
System.out.println(student);
}
// find a student by id
Student student = dao.findById(3);
System.out.println("Student with id=3: " + student);
// display all students
students = dao.findAll();
System.out.println("All students:");
for (Student s : students) {
System.out.println(s);
}
// display students in a dialog
StringBuilder sb = new StringBuilder();
for (Student s : students) {
sb.append(s).append("\n");
}
JOptionPane.showMessageDialog(null, sb.toString(), "All students", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
```
这个示例代码使用MySQL数据库,需要先安装MySQL并创建一个名为"stu"的数据库。在运行Main类之前,需要确保MySQL服务已经启动,并修改url、username和password变量为正确的值。
阅读全文