做一个java的学生管理系统,信息存储到数据库里
时间: 2023-05-26 21:01:44 浏览: 125
A: 好的,我可以帮你设计一个Java的学生管理系统,信息将会存储到数据库里。我们可以使用MySQL数据库进行存储操作。首先,我们需要定义学生类,包含姓名、年龄、性别、学号等信息。
```
public class Student {
private int id;
private String name;
private int age;
private char gender;
private String studentId;
public Student(int id, String name, int age, char gender, String studentId) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.studentId = studentId;
}
// getters and setters
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public char getGender() {
return gender;
}
public String getStudentId() {
return studentId;
}
}
```
接下来,我们需要建立与数据库的连接,并创建学生信息表。我们可以使用JDBC连接MySQL数据库,以下是一个简单的数据库工具类。
```
import java.sql.*;
public class DBUtil {
private static Connection connection;
private static Connection getConnection(){
if (connection == null) {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/Student";
String username = "root";
String password = "password";
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
public static void closeConnection(){
try {
if(connection != null)
connection.close();
}catch (SQLException se){
se.printStackTrace();
}
}
public static Statement getStatement(){
Statement stmt = null;
try{
stmt = getConnection().createStatement();
}catch(SQLException se){
se.printStackTrace();
}
return stmt;
}
public static PreparedStatement getPreparedStatement(String sql) {
PreparedStatement ps = null;
try {
ps = getConnection().prepareStatement(sql);
} catch (SQLException se) {
se.printStackTrace();
}
return ps;
}
public static ResultSet executeQuery(String sql) {
Statement stmt = getStatement();
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException se) {
se.printStackTrace();
}
return rs;
}
public static int executeUpdate(String sql) {
Statement stmt = getStatement();
int result = 0;
try {
result = stmt.executeUpdate(sql);
} catch (SQLException se) {
se.printStackTrace();
}
return result;
}
}
```
有了以上的工具类后,我们可以创建数据库表,以下是一个简单的建表语句:
```
CREATE TABLE student (
id int(11),
name varchar(50),
age int(11),
gender char(1),
studentId varchar(50)
);
```
接下来,我们可以定义一个StudentDAO类,用来对学生信息进行CRUD操作。
```
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
public static void addStudent(Student stu) {
String sql = "INSERT INTO student(id, name, age, gender, studentId) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = DBUtil.getPreparedStatement(sql);
try {
ps.setInt(1, stu.getId());
ps.setString(2, stu.getName());
ps.setInt(3, stu.getAge());
ps.setString(4, String.valueOf(stu.getGender()));
ps.setString(5, stu.getStudentId());
ps.executeUpdate();
} catch (SQLException se) {
se.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException se) {
se.printStackTrace();
}
DBUtil.closeConnection();
}
}
public static void deleteStudent(int id) {
String sql = "DELETE FROM student WHERE id = ?";
PreparedStatement ps = DBUtil.getPreparedStatement(sql);
try {
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException se) {
se.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException se) {
se.printStackTrace();
}
DBUtil.closeConnection();
}
}
public static void updateStudent(Student stu) {
String sql = "UPDATE student SET name = ?, age = ?, gender = ?, studentId = ? WHERE id = ?";
PreparedStatement ps = DBUtil.getPreparedStatement(sql);
try {
ps.setString(1, stu.getName());
ps.setInt(2, stu.getAge());
ps.setString(3, String.valueOf(stu.getGender()));
ps.setString(4, stu.getStudentId());
ps.setInt(5, stu.getId());
ps.executeUpdate();
} catch (SQLException se) {
se.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException se) {
se.printStackTrace();
}
DBUtil.closeConnection();
}
}
public static List<Student> getAllStudents() {
String sql = "SELECT * FROM student";
List<Student> students = new ArrayList<>();
ResultSet rs = DBUtil.executeQuery(sql);
try {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
char gender = rs.getString("gender").charAt(0);
String studentId = rs.getString("studentId");
Student stu = new Student(id, name, age, gender, studentId);
students.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
DBUtil.closeConnection();
}
return students;
}
public static Student getStudentById(int id) {
String sql = "SELECT * FROM student WHERE id = ?";
PreparedStatement ps = DBUtil.getPreparedStatement(sql);
ResultSet rs = null;
Student stu = null;
try {
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
char gender = rs.getString("gender").charAt(0);
String studentId = rs.getString("studentId");
stu = new Student(id, name, age, gender, studentId);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
ps.close();
} catch (SQLException se) {
se.printStackTrace();
}
DBUtil.closeConnection();
}
return stu;
}
}
```
这样一个简单的学生管理系统就设计完毕了,你可以根据需要进行改进,添加各种功能。
阅读全文
相关推荐


















