帮我写一个jdbc的学生管理系统,需要包含学生登录、注册、根据姓名模糊查询,修改学生信息,根据学号删除学生信息。只需要在控制台显示,字段有学号、用户名、真实名、密码、年龄、性别
时间: 2023-10-20 22:26:26 浏览: 80
好的,下面是一个简单的 JDBC 学生管理系统的代码,仅供参考:
```java
import java.sql.*;
public class StudentManager {
// 数据库连接信息
private static final String DB_URL = "jdbc:mysql://localhost:3306/student_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";
// 数据库表中的字段
private static final String ID = "id";
private static final String USERNAME = "username";
private static final String REALNAME = "realname";
private static final String PASSWORD = "password";
private static final String AGE = "age";
private static final String GENDER = "gender";
// 创建数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
}
// 学生登录
public static boolean login(String username, String password) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE username=? AND password=?")) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 学生注册
public static boolean register(String username, String realname, String password, int age, String gender) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (username, realname, password, age, gender) VALUES (?, ?, ?, ?, ?)")) {
stmt.setString(1, username);
stmt.setString(2, realname);
stmt.setString(3, password);
stmt.setInt(4, age);
stmt.setString(5, gender);
return stmt.executeUpdate() == 1;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 根据姓名模糊查询
public static void searchByName(String name) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE realname LIKE ?")) {
stmt.setString(1, "%" + name + "%");
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.printf("ID: %s, Username: %s, Realname: %s, Password: %s, Age: %d, Gender: %s\n",
rs.getString(ID), rs.getString(USERNAME), rs.getString(REALNAME),
rs.getString(PASSWORD), rs.getInt(AGE), rs.getString(GENDER));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改学生信息
public static boolean update(String id, String username, String realname, String password, int age, String gender) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("UPDATE students SET username=?, realname=?, password=?, age=?, gender=? WHERE id=?")) {
stmt.setString(1, username);
stmt.setString(2, realname);
stmt.setString(3, password);
stmt.setInt(4, age);
stmt.setString(5, gender);
stmt.setString(6, id);
return stmt.executeUpdate() == 1;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 根据学号删除学生信息
public static boolean delete(String id) {
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("DELETE FROM students WHERE id=?")) {
stmt.setString(1, id);
return stmt.executeUpdate() == 1;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
// 测试代码
System.out.println(login("user1", "password1")); // true
System.out.println(login("user1", "wrong_password")); // false
System.out.println(register("user3", "张三", "password3", 20, "男")); // true
searchByName("张");
System.out.println(update("1", "user1_updated", "李四", "password1_updated", 25, "女")); // true
System.out.println(delete("2")); // true
}
}
```
注意:以上代码仅供参考,实际使用中需要根据具体情况进行修改和优化。同时,为了保证安全性,建议使用加密算法对密码进行加密存储。
阅读全文