/**
* Created with IntelliJ IDEA.
* Description:
* User: LC007
* Date: 2024-06-22
* Time: 21:15
*/
package zjz;
import java.util.List;
import java.util.Scanner;
import zjz.Student;
import zjz.StudentDao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
public class Main1 {
public static void main(String[] args) {
Main1 m=new Main1();
m.menu();
}
public void menu() {
// 1.打印菜单
// 2.输入菜单
// 3.switch 菜单选择
int choose;
do {
System.out.println("******************************");
System.out.println("===欢迎进入学生信息管理系统===");
System.out.println("1.新增学生");
System.out.println("2.修改学生");
System.out.println("3.删除学生");
System.out.println("4.查询学生");
System.out.println("5.退出该系统");
System.out.println("请选择(1-5):");
Scanner scanner = new Scanner(System.in);
choose = scanner.nextInt();
System.out.println("******************************");
switch (choose) {
case 1: myAdd() ;// 菜单选择 1,是新增学生
break;
case 2: myUpdate(); // 菜单选择 2,是修改学生
break;
case 3: myDel(); // 菜单选择 3,是删除学生
break;
case 4: myList(); // 菜单选择 4,是查询学生
break;
case 5: // 菜单选择 5,是退出该系统
System.out.println("您选择了退出系统,确定要退出吗?(y/n)");
Scanner scan = new Scanner(System.in);
String scanExit = scan.next();
if (scanExit.equalsIgnoreCase("y")) {
System.out.println("您已成功退出系统,欢迎您再次使用!");
System.exit(-1);
}
break;
default: break;
}
} while (choose != 5);
}
// 新增学生信息
public void myAdd() {
Scanner scanner = new Scanner(System.in);
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjz", "root", "zjz123456");
StudentDao dao = new StudentDao(conn);
System.out.println("====新增学生====");
System.out.println("学号:");
String number = scanner.next();
System.out.println("姓名:");
String name = scanner.next();
System.out.println("性别:");
String sex = scanner.next();
System.out.println("专业:");
String major = scanner.next();
System.out.println("成绩:");
int score = scanner.nextInt();
Student stu = new Student(number, name, sex, major, score);
boolean ok = dao.add(stu);
if (ok) {
System.out.println("保存成功!");
} else {
System.out.println("保存失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
// 删除学生信息
public void myDel() {
Scanner scanner = new Scanner(System.in);
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjz", "root", "zjz123456");
StudentDao dao = new StudentDao(conn);
System.out.println("====删除学生====");
System.out.print("请输入要删除的学生学号:");
String number = scanner.next();
Student stu = dao.findOne(number);
if (stu == null) {
System.out.println("未找到学号为 " + number + " 的学生!");
return;
}
System.out.println("找到的学生信息如下:");
System.out.println("学生学号:" + stu.getNumber());
System.out.println("学生姓名:" + stu.getName());
System.out.println("学生性别:" + stu.getSex());
System.out.println("学生专业:" + stu.getMajor());
System.out.println("学生成绩:" + stu.getScore());
System.out.print("是否真的删除(y/n):");
String confirm = scanner.next();
if ("y".equalsIgnoreCase(confirm)) {
boolean ok = dao.del(number);
if (ok) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
// 修改学生信息
public void myUpdate() {
Scanner scanner = new Scanner(System.in);
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjz", "root", "zjz123456");
StudentDao dao = new StudentDao(conn);
System.out.println("====修改学生====");
System.out.print("请输入要修改的学生学号:");
String number = scanner.next();
Student stu = dao.findOne(number);
if (stu == null) {
System.out.println("未找到学号为 " + number + " 的学生!");
return;
}
System.out.println("该学生的信息如下:");
System.out.println("学生学号:" + stu.getNumber());
System.out.println("学生姓名:" + stu.getName());
System.out.println("学生性别:" + stu.getSex());
System.out.println("学生专业:" + stu.getMajor());
System.out.println("学生成绩:" + stu.getScore());
System.out.print("请输入新的学生姓名:");
String name = scanner.next();
System.out.print("请输入新的学生性别:");
String sex = scanner.next();
System.out.print("请输入新的学生专业:");
String major = scanner.next();
System.out.print("请输入新的学生成绩:");
int score = scanner.nextInt();
stu.setName(name);
stu.setSex(sex);
stu.setMajor(major);
stu.setScore(score);
boolean ok = dao.update(stu);
if (ok) {
System.out.println("学生信息更新成功!");
} else {
System.out.println("学生信息更新失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
scanner.close();
}
}
// 查询学生信息
public void myList() {
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zjz", "root", "zjz123456");
StudentDao dao = new StudentDao(conn);
System.out.println("******************************");
System.