帮我写一个java学生信息管理系统(带数据库)的代码
时间: 2023-08-28 09:15:39 浏览: 70
好的,这是一个简单的Java学生信息管理系统的代码,带有数据库存储功能。由于代码比较长,我会分步骤给出。
第一步:创建数据库表
在本例中,我们创建了一个名为“students”的表,其中包含以下字段:id(学生ID)、name(学生姓名)、age(学生年龄)、gender(学生性别)和major(学生专业)。
```sql
CREATE TABLE students (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT(11) NOT NULL,
gender VARCHAR(10) NOT NULL,
major VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
```
第二步:连接数据库
```java
import java.sql.*;
public class DatabaseConnection {
public static void main(String[] args) {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 打印连接成功信息
System.out.println("Database connected successfully");
// 关闭连接
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
```
请注意,上面的代码中,“test”是数据库的名称,“root”是用户名,“password”是密码。如果您的设置不同,请相应地更改。
第三步:添加学生信息
```java
import java.sql.*;
public class AddStudent {
public static void main(String[] args) {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建 PreparedStatement 对象并设置 SQL 语句
PreparedStatement pst = con.prepareStatement("INSERT INTO students(name, age, gender, major) VALUES (?, ?, ?, ?)");
// 设置占位符参数的值
pst.setString(1, "Lucy");
pst.setInt(2, 20);
pst.setString(3, "Female");
pst.setString(4, "Computer Science");
// 执行 SQL 语句并获取受影响的行数
int rows = pst.executeUpdate();
// 打印添加成功信息
System.out.println(rows + " row(s) affected");
// 关闭连接
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
```
在上面的代码中,我们使用了 PreparedStatement 对象来防止 SQL 注入攻击,并通过占位符设置参数的值。
第四步:查询学生信息
```java
import java.sql.*;
public class QueryStudent {
public static void main(String[] args) {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建 Statement 对象并设置 SQL 语句
Statement stmt = con.createStatement();
String sql = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(sql);
// 遍历结果集并打印学生信息
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String major = rs.getString("major");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age + ", Gender: " + gender + ", Major: " + major);
}
// 关闭连接
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
```
在上面的代码中,我们使用了 Statement 对象执行 SQL 查询语句,并通过 ResultSet 对象遍历结果集。
第五步:更新学生信息
```java
import java.sql.*;
public class UpdateStudent {
public static void main(String[] args) {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建 PreparedStatement 对象并设置 SQL 语句
PreparedStatement pst = con.prepareStatement("UPDATE students SET name=?, age=?, gender=?, major=? WHERE id=?");
// 设置占位符参数的值
pst.setString(1, "Lucy Smith");
pst.setInt(2, 21);
pst.setString(3, "Female");
pst.setString(4, "Computer Science");
pst.setInt(5, 1);
// 执行 SQL 语句并获取受影响的行数
int rows = pst.executeUpdate();
// 打印更新成功信息
System.out.println(rows + " row(s) affected");
// 关闭连接
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
```
在上面的代码中,我们使用了 PreparedStatement 对象并通过占位符设置参数的值来更新学生信息。
第六步:删除学生信息
```java
import java.sql.*;
public class DeleteStudent {
public static void main(String[] args) {
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
// 创建 PreparedStatement 对象并设置 SQL 语句
PreparedStatement pst = con.prepareStatement("DELETE FROM students WHERE id=?");
// 设置占位符参数的值
pst.setInt(1, 1);
// 执行 SQL 语句并获取受影响的行数
int rows = pst.executeUpdate();
// 打印删除成功信息
System.out.println(rows + " row(s) affected");
// 关闭连接
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
```
在上面的代码中,我们使用了 PreparedStatement 对象并通过占位符设置参数的值来删除学生信息。
以上是一个简单的Java学生信息管理系统的代码,带有数据库存储功能。由于代码比较长,我仅提供了基本的增删改查操作。如果您需要更多的功能,请自行扩展。