实验内容
-
设计一个简单的学生信息管理的程序 StudentInfo ,教学管理人员能够使用 StudentInfo 程序对学生基本信息、课程信息进行管理,包括数据的添加、修改删除和浏览;能够对学生选课进行管理,包括添加学生选课信息、录入成绩;能使用查询功能,快速查看到指定学生的选课信息;能够对学生选课情况进行简单的统计,包括所选的总的课程数、总学分数及平均成绩。
-
在添加学生基本信息、课程基本信息相关数据时,学号和课程号不能重复;还有在添加学生选课信息时,要求该学生和课程必须是存在的,而且不能添加重复的选课信息。
-
应用程序提供操作界面,可以方便用户进行功能选择,实现信息的管理和查询,并可以清晰地显示相关信息。
相关知识
- Java继承类与接口技术,console控制台的输入与输出。
- MySQL 的相关知识,包含数据库的连接、路径、状态、结果集等。
- 控制台的输入后续与连续输出显示,界面友好型的异常捕捉机制等。
具体步骤
①安装MySQL,参考:https://downloads.mysql.com/archives/installer/
②把 MySQL 路径里的 connectorJ 文件夹里的 jar文件添加到所使用的 IDE 创建路径里。这里演示的是 eclipse。
③连接MySQL
// mysql的数据库路径
String dburl = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
// 定义数据库连接
Connection conn = null;
// 定义数据库状态
PreparedStatement stmt = null;
// 定义数据库返回结果集
ResultSet rs = null;
④完整代码
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class database {
public static void main(String[] args) throws SQLException {
// mysql的数据库路径
String dburl = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
// 定义数据库连接
Connection conn = null;
// 定义数据库状态
PreparedStatement stmt = null;
// 定义数据库返回结果集
ResultSet rs = null;
while (true) {
System.out.println("1--插入\n" + "2--查找\n" + "3--修改\n" + "4--删除\n" + "5--退出");
System.out.println("请选择一个数字,进行操作");
Scanner sc = new Scanner(System.in);
try {
String a = sc.next();
switch (a) {
// 增
case "1": {
System.out.println("请输入需要插入的学号、姓名、语文、数学、英语:");
String insert_stuid = sc.next();
String insert_stuname = sc.next();
double insert_math = sc.nextDouble();
double insert_english = sc.nextDouble();
double insert_history = sc.nextDouble();
try {
// 加载mysql驱动
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, "root", "gyf493GYF");
String sql = "select * from stuinfo where 学号 = ?";
stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.setString(1, insert_stuid);
rs = stmt.executeQuery();
if (!rs.next()) {
String insertsql = "insert into stuinfo values(?,?,?,?,?)";
stmt = conn.prepareStatement(insertsql);
stmt.setString(1, insert_stuid);
stmt.setString(2, insert_stuname);
stmt.setDouble(3, insert_math);
stmt.setDouble(4, insert_english);
stmt.setDouble(5, insert_history);
stmt.executeUpdate();
System.out.println("新记录已入库");
} else
System.out.println("已有记录,不能重复");
System.out.println("-----------------------------------");
} catch (SQLException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库加载驱动程序");
} finally {
rs.close();
stmt.close();
conn.close();
}
break;
}
// 查
case "2": {
int count = 0;
String stuid_key = "100";
double math_key = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, "root", "gyf493GYF");
String sql = "select * from stuinfo where 学号 > ? and 数学 > ? " + "order by 学号 asc";
stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.setString(1, stuid_key);
stmt.setDouble(2, math_key);
rs = stmt.executeQuery();
System.out.println("学号\t" + "姓名\t" + "语文\t" + "数学\t" + "英语");
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2) +
"\t" + rs.getDouble(3) + "\t"
+ rs.getDouble(4) + "\t" + rs.getDouble(5));
}
rs.previous();
count = rs.getRow();
System.out.println("共检索出" + count + "条记录");
System.out.println("-----------------------------------");
} catch (SQLException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库加载驱动程序");
} finally {
rs.close();
stmt.close();
conn.close();
}
break;
}
// 改
case "3": {
System.out.println("请输入需要被更新的学生的学号");
String delete_stuid = sc.next();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, "root", "gyf493GYF");
String deletesql = "delete from stuinfo where 学号 = ?";
stmt = conn.prepareStatement(deletesql);
stmt.setString(1, delete_stuid);
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库加载驱动程序");
}
System.out.println("请输入更新后的学号、姓名、语文、数学、英语:");
String insert_stuid = sc.next();
String insert_stuname = sc.next();
double insert_math = sc.nextDouble();
double insert_english = sc.nextDouble();
double insert_history = sc.nextDouble();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, "root", "gyf493GYF");
String sql = "select * from stuinfo where 学号 = ?";
stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
stmt.setString(1, insert_stuid);
rs = stmt.executeQuery();
if (!rs.next()) {
String insertsql = "insert into stuinfo values(?,?,?,?,?)";
stmt = conn.prepareStatement(insertsql);
stmt.setString(1, insert_stuid);
stmt.setString(2, insert_stuname);
stmt.setDouble(3, insert_math);
stmt.setDouble(4, insert_english);
stmt.setDouble(5, insert_history);
stmt.executeUpdate();
System.out.println("成功更新!");
System.out.println("-----------------------------------");
}
} catch (SQLException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库加载驱动程序");
} finally {
rs.close();
stmt.close();
conn.close();
}
break;
}
// 删
case "4": {
System.out.println("请输入需要删除的学号:");
String delete_stuid = sc.next();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, "root", "gyf493GYF");
String deletesql = "delete from stuinfo where 学号 = ?";
stmt = conn.prepareStatement(deletesql);
stmt.setString(1, delete_stuid);
int delete_count = stmt.executeUpdate();
System.out.println("共" + delete_count + "条记录被删除");
System.out.println("-----------------------------------");
} catch (SQLException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库加载驱动程序");
} finally {
stmt.close();
conn.close();
}
break;
}
case "5": {
System.out.println("您已成功退出!");
return;
}
default:
System.out.println("输入数据错误,请重新输入!");
System.out.println("-----------------------------------");
break;
}
} catch (InputMismatchException e) {
System.out.println(e.getMessage());
}
}
}
}