创建一个JDBC类,其主要包括了创建与数据库的连接,查询全部学生信息,添加学生信息,修改学生信息和删除学生信息等方法并将返回的结果集封装到ArrayList中
时间: 2024-03-07 21:50:29 浏览: 75
数据库课程设计学生信息管理系统的设计与实现
3星 · 编辑精心推荐
好的,以下是一个简单的 JDBC 工具类,包含了连接数据库、查询全部学生信息、添加学生信息、修改学生信息和删除学生信息的方法,并将返回的结果集封装到 ArrayList 中:
```java
import java.sql.*;
import java.util.ArrayList;
public class JDBCUtil {
// 数据库连接信息
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASSWORD = "root";
// 加载数据库驱动
static {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 获取数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// 查询全部学生信息
public static ArrayList<Student> getAllStudents() throws SQLException {
ArrayList<Student> students = new ArrayList<>();
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student")) {
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGrade(rs.getInt("grade"));
students.add(student);
}
}
return students;
}
// 添加学生信息
public static boolean addStudent(Student student) throws SQLException {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO student (name, age, grade) VALUES (?, ?, ?)")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setInt(3, student.getGrade());
return stmt.executeUpdate() > 0;
}
}
// 修改学生信息
public static boolean updateStudent(Student student) throws SQLException {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE student SET name=?, age=?, grade=? WHERE id=?")) {
stmt.setString(1, student.getName());
stmt.setInt(2, student.getAge());
stmt.setInt(3, student.getGrade());
stmt.setInt(4, student.getId());
return stmt.executeUpdate() > 0;
}
}
// 删除学生信息
public static boolean deleteStudent(int id) throws SQLException {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM student WHERE id=?")) {
stmt.setInt(1, id);
return stmt.executeUpdate() > 0;
}
}
}
```
其中,`Student` 是一个简单的 JavaBean,包含了 id、name、age 和 grade 四个属性。
```java
public class Student {
private int id;
private String name;
private int age;
private int grade;
// getter 和 setter 方法省略...
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", grade=" + grade +
'}';
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) throws SQLException {
// 查询全部学生信息
ArrayList<Student> students = JDBCUtil.getAllStudents();
System.out.println("All students: " + students);
// 添加学生信息
Student newStudent = new Student();
newStudent.setName("Tom");
newStudent.setAge(20);
newStudent.setGrade(90);
System.out.println("Add student result: " + JDBCUtil.addStudent(newStudent));
// 查询全部学生信息
students = JDBCUtil.getAllStudents();
System.out.println("All students: " + students);
// 修改学生信息
Student updateStudent = students.get(0);
updateStudent.setName("Jerry");
updateStudent.setAge(21);
updateStudent.setGrade(80);
System.out.println("Update student result: " + JDBCUtil.updateStudent(updateStudent));
// 查询全部学生信息
students = JDBCUtil.getAllStudents();
System.out.println("All students: " + students);
// 删除学生信息
int deleteStudentId = students.get(0).getId();
System.out.println("Delete student result: " + JDBCUtil.deleteStudent(deleteStudentId));
// 查询全部学生信息
students = JDBCUtil.getAllStudents();
System.out.println("All students: " + students);
}
}
```
阅读全文