有Java和MySQL
时间: 2024-03-09 08:47:55 浏览: 55
好的,以下是一个使用Java和MySQL实现的学生成绩信息管理系统代码示例:
首先,需要在MySQL中创建一个名为“students”的数据库,并创建一个名为“student_scores”的数据表,其中包含“name”(姓名)、“id”(学生ID)和“marks”(成绩)三个字段。
然后,需要在Java中使用JDBC连接到MySQL数据库,并编写相应的Java代码实现学生成绩信息的添加、查看、搜索和删除功能。
具体代码如下:
```
import java.sql.*;
public class StudentScoreManagementSystem {
private static final String DB_URL = "jdbc:mysql://localhost:3306/students";
private static final String USER = "root";
private static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
int choice;
do {
System.out.println("\n1. Add student\n");
System.out.println("2. View all students\n");
System.out.println("3. Search for a student\n");
System.out.println("4. Delete a student\n");
System.out.println("5. Exit\n");
System.out.println("Enter your choice: ");
choice = Integer.parseInt(System.console().readLine());
switch (choice) {
case 1:
System.out.println("Enter student name: ");
String name = System.console().readLine();
System.out.println("Enter student ID: ");
int id = Integer.parseInt(System.console().readLine());
System.out.println("Enter student marks: ");
int marks = Integer.parseInt(System.console().readLine());
String sql = "INSERT INTO student_scores (name, id, marks) VALUES ('" + name + "', " + id + ", " + marks + ")";
stmt.executeUpdate(sql);
System.out.println("Student added successfully!");
break;
case 2:
sql = "SELECT * FROM student_scores";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Name\tID\tMarks");
while (rs.next()) {
String studentName = rs.getString("name");
int studentID = rs.getInt("id");
int studentMarks = rs.getInt("marks");
System.out.println(studentName + "\t" + studentID + "\t" + studentMarks);
}
rs.close();
break;
case 3:
System.out.println("Enter student name to search for: ");
String searchName = System.console().readLine();
sql = "SELECT * FROM student_scores WHERE name='" + searchName + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
int studentID = rs.getInt("id");
int studentMarks = rs.getInt("marks");
System.out.println("Name\tID\tMarks");
System.out.println(searchName + "\t" + studentID + "\t" + studentMarks);
} else {
System.out.println("Student not found!");
}
rs.close();
break;
case 4:
System.out.println("Enter student ID to delete: ");
int deleteID = Integer.parseInt(System.console().readLine());
sql = "DELETE FROM student_scores WHERE id=" + deleteID;
int rowsDeleted = stmt.executeUpdate(sql);
if (rowsDeleted > 0) {
System.out.println("Student deleted successfully!");
} else {
System.out.println("Student not found!");
}
break;
case 5:
System.out.println("Exiting program... Goodbye!");
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 5);
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
```
该程序使用JDBC连接到MySQL数据库,并提供了以下功能:
1. 添加学生。
2. 查看所有学生。
3. 搜索学生。
4. 删除学生。
5. 退出程序。
通过以上Java和MySQL代码示例,您可以自行修改并完善学生成绩信息管理系统。
阅读全文