使用Derby数据库或者其它数据库,在数据库中,设计一张表,包括学号、姓名、年龄、班级等信息。使用Java编程,连接该数据库,写入、读出、修改、查询学生信息。
时间: 2024-04-08 21:35:18 浏览: 61
好的,以下是使用Derby数据库实现你所描述的功能的示例代码:
```java
import java.sql.*;
public class StudentDatabase {
private Connection connection;
private Statement statement;
public void connect() {
try {
// 连接到Derby数据库
connection = DriverManager.getConnection("jdbc:derby:studentDB;create=true");
statement = connection.createStatement();
// 创建学生表
String createTableQuery = "CREATE TABLE Students ("
+ "id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
+ "studentNumber INT,"
+ "name VARCHAR(50),"
+ "age INT,"
+ "class VARCHAR(50)"
+ ")";
statement.executeUpdate(createTableQuery);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertStudent(int studentNumber, String name, int age, String className) {
try {
// 插入学生信息
String insertQuery = "INSERT INTO Students (studentNumber, name, age, class) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setInt(1, studentNumber);
preparedStatement.setString(2, name);
preparedStatement.setInt(3, age);
preparedStatement.setString(4, className);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateStudent(int studentNumber, String newName) {
try {
// 更新学生姓名
String updateQuery = "UPDATE Students SET name = ? WHERE studentNumber = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updateQuery);
preparedStatement.setString(1, newName);
preparedStatement.setInt(2, studentNumber);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteStudent(int studentNumber) {
try {
// 删除学生信息
String deleteQuery = "DELETE FROM Students WHERE studentNumber = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery);
preparedStatement.setInt(1, studentNumber);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void queryStudent(int studentNumber) {
try {
// 查询学生信息
String selectQuery = "SELECT * FROM Students WHERE studentNumber = ?";
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, studentNumber);
ResultSet resultSet = preparedStatement.executeQuery();
// 打印查询结果
if (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String className = resultSet.getString("class");
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Class: " + className);
} else {
System.out.println("No student found with student number: " + studentNumber);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close() {
try {
// 关闭连接
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentDatabase studentDatabase = new StudentDatabase();
studentDatabase.connect();
// 插入学生信息
studentDatabase.insertStudent(1001, "张三", 20, "Class A");
studentDatabase.insertStudent(1002, "李四", 21, "Class B");
// 查询学生信息
studentDatabase.queryStudent(1001);
studentDatabase.queryStudent(1002);
// 更新学生信息
studentDatabase.updateStudent(1001, "王五");
// 查询学生信息
studentDatabase.queryStudent(1001);
// 删除学生信息
studentDatabase.deleteStudent(1002);
// 查询学生信息
studentDatabase.queryStudent(1002);
studentDatabase.close();
}
}
```
这个示例代码通过Java连接Derby数据库,创建了一个名为"Students"的表,包括学号、姓名、年龄、班级等字段。然后提供了插入、查询、更新和删除学生信息的方法,最后演示了如何使用这些方法。请根据你的需求进行修改和扩展。
阅读全文