运用java做一个学生登陆页面,进去后可以添加,删除,修改,查询学生成绩系统代码
时间: 2024-03-25 07:39:34 浏览: 122
好的,下面是一个简单的Java代码示例,实现了学生登陆页面和学生成绩系统的添加、删除、修改、查询功能。需要注意的是,这个示例代码仅供参考,实际应用中需要进行适当的修改和优化。
```
import java.sql.*;
import java.util.Scanner;
public class StudentGradeSystem {
// 数据库连接相关信息
private static final String DB_URL = "jdbc:mysql://localhost:3306/student_grade_system";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "123456";
public static void main(String[] args) {
// 学生登陆页面
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
if (!login(username, password)) {
System.out.println("用户名或密码错误!");
return;
}
// 学生成绩系统
System.out.println("欢迎进入学生成绩系统!");
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加学生成绩");
System.out.println("2. 删除学生成绩");
System.out.println("3. 修改学生成绩");
System.out.println("4. 查询学生成绩");
System.out.println("5. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addGrade();
break;
case 2:
deleteGrade();
break;
case 3:
modifyGrade();
break;
case 4:
queryGrade();
break;
case 5:
System.out.println("感谢使用学生成绩系统!");
return;
default:
System.out.println("无效的选择!");
}
}
}
// 学生登陆功能
private static boolean login(String username, String password) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
statement = connection.createStatement();
String sql = "SELECT * FROM student WHERE username = '" + username + "' AND password = '" + password + "'";
resultSet = statement.executeQuery(sql);
return resultSet.next();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
// 添加学生成绩功能
private static void addGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
System.out.println("请输入语文成绩:");
double chinese = scanner.nextDouble();
System.out.println("请输入数学成绩:");
double math = scanner.nextDouble();
System.out.println("请输入英语成绩:");
double english = scanner.nextDouble();
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "INSERT INTO grade (name, id, chinese, math, english) VALUES (?, ?, ?, ?, ?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setString(2, id);
preparedStatement.setDouble(3, chinese);
preparedStatement.setDouble(4, math);
preparedStatement.setDouble(5, english);
int result = preparedStatement.executeUpdate();
if (result > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 删除学生成绩功能
private static void deleteGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "DELETE FROM grade WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
int result = preparedStatement.executeUpdate();
if (result > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 修改学生成绩功能
private static void modifyGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = scanner.nextLine();
System.out.println("请输入新的语文成绩:");
double chinese = scanner.nextDouble();
System.out.println("请输入新的数学成绩:");
double math = scanner.nextDouble();
System.out.println("请输入新的英语成绩:");
double english = scanner.nextDouble();
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "UPDATE grade SET chinese = ?, math = ?, english = ? WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setDouble(1, chinese);
preparedStatement.setDouble(2, math);
preparedStatement.setDouble(3, english);
preparedStatement.setString(4, id);
int result = preparedStatement.executeUpdate();
if (result > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 查询学生成绩功能
private static void queryGrade() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生学号:");
String id = scanner.nextLine();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "SELECT * FROM grade WHERE id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, id);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
System.out.println("姓名:" + resultSet.getString("name"));
System.out.println("学号:" + resultSet.getString("id"));
System.out.println("语文成绩:" + resultSet.getDouble("chinese"));
System.out.println("数学成绩:" + resultSet.getDouble("math"));
System.out.println("英语成绩:" + resultSet.getDouble("english"));
} else {
System.out.println("查询失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
```
上述代码中,我们使用了Java语言和MySQL数据库来实现学生成绩系统,其中包括了学生登陆页面和添加、删除、修改、查询学生成绩的功能。需要注意的是,该示例代码仅供参考,实际应用中需要进行适当的修改和优化。
阅读全文