使用java编写程序实现创建一学生数据库School,创建一表studb,包含字段:学号、姓名、性别、专业、入学年份。编写程序对studb表进行学生信息的增、删、改、查操作
时间: 2023-10-19 08:06:03 浏览: 119
好的,这是一个比较常见的数据库操作,以下是Java代码示例,需要使用 JDBC 连接数据库:
1. 导入 JDBC 驱动包
```java
Class.forName("com.mysql.jdbc.Driver");
```
2. 建立数据库连接
```java
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/School", "root", "password");
```
3. 创建 SQL 语句并执行
- 增加学生信息
```java
String sql = "INSERT INTO studb (id, name, gender, major, enrollment_year) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1001");
pstmt.setString(2, "张三");
pstmt.setString(3, "男");
pstmt.setString(4, "计算机科学");
pstmt.setString(5, "2020");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学生信息添加成功!");
}
```
- 删除学生信息
```java
String sql = "DELETE FROM studb WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1001");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学号为 1001 的学生信息删除成功!");
}
```
- 修改学生信息
```java
String sql = "UPDATE studb SET name = ?, gender = ?, major = ?, enrollment_year = ? WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "李四");
pstmt.setString(2, "女");
pstmt.setString(3, "软件工程");
pstmt.setString(4, "2019");
pstmt.setString(5, "1002");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学生信息修改成功!");
}
```
- 查询学生信息
```java
String sql = "SELECT * FROM studb WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1002");
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
System.out.println("学号:" + rs.getString("id") +
", 姓名:" + rs.getString("name") +
", 性别:" + rs.getString("gender") +
", 专业:" + rs.getString("major") +
", 入学年份:" + rs.getString("enrollment_year"));
}
```
完整代码如下:
```java
import java.sql.*;
public class StudentDatabase {
public static void main(String[] args) {
Connection conn = null;
try {
// 1. 加载 JDBC 驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 建立数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/School", "root", "password");
// 增加学生信息
String sql = "INSERT INTO studb (id, name, gender, major, enrollment_year) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1001");
pstmt.setString(2, "张三");
pstmt.setString(3, "男");
pstmt.setString(4, "计算机科学");
pstmt.setString(5, "2020");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学生信息添加成功!");
}
// 删除学生信息
sql = "DELETE FROM studb WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1001");
result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学号为 1001 的学生信息删除成功!");
}
// 修改学生信息
sql = "UPDATE studb SET name = ?, gender = ?, major = ?, enrollment_year = ? WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "李四");
pstmt.setString(2, "女");
pstmt.setString(3, "软件工程");
pstmt.setString(4, "2019");
pstmt.setString(5, "1002");
result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("学生信息修改成功!");
}
// 查询学生信息
sql = "SELECT * FROM studb WHERE id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "1002");
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
System.out.println("学号:" + rs.getString("id") +
", 姓名:" + rs.getString("name") +
", 性别:" + rs.getString("gender") +
", 专业:" + rs.getString("major") +
", 入学年份:" + rs.getString("enrollment_year"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
注意,这里的 `jdbc:mysql://localhost:3306/School` 需要替换成你自己的 MySQL 数据库地址,以及 `root` 和 `password` 需要替换成你自己的 MySQL 数据库的用户名和密码。此外,你需要在 MySQL 中创建一个名为 `School` 的数据库,并在其中创建一个名为 `studb` 的表,包含字段:`id`、`name`、`gender`、`major`、`enrollment_year`。
阅读全文