public class Dream {
static String name;
static String id;
static String sql;
static boolean flag;
public static void main(String[] args) {
for (;;) {
System.out.println("*******************************");
System.out.println("* *");
System.out.println("* 欢迎进入学生信息管理系统 *");
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("请输入操作序号,以Enter结束:");
Scanner scanner = new Scanner(System.in);
int j = scanner.nextInt();
switch (j) {
case 1: {
System.out.println("请输入学生ID:");
id = scanner.next();
if (DBUtil.exist("select *from student where id=?", id)) {
System.out.println("id已经存在:");
break;
} else {
System.out.println("请输入学生姓名:");
name = scanner.next();
if(DBUtil.apply("insert into student(id,name) values(?,?)",id,name)) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
break;
}
}
case 2: {
System.out.println("请输出需要删除的学生ID:");
id=scanner.next();
if(!DBUtil.exist("select *from student where id=?", id)) {
System.out.println("不存在该ID");
break;
}else {
if(DBUtil.apply("delete from student where id=?",id)) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
break;
}
case 3: {
System.out.println("请输入学生修改ID:");
id = scanner.next();
if(!DBUtil.exist("select *from student where id=?", id)) {
System.out.println("不存在该ID");
break;
}else {
System.out.println("请输入新的学生姓名:");
name = scanner.next();
if(DBUtil.apply("update student set name=? where id=?",name,id)) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
}
break;
}
case 4: {
System.out.println("请输入查询学生的学号");
id =scanner.next();
DBUtil.select("select *from student where id=?", (resultSet)->{
try {
if (resultSet.next()) {
id = resultSet.getString("id");
name = resultSet.getString("name");
System.out.println("id=" + id + ",name=" + name);
} else {
System.out.println("ID不存在");
}
} catch (Exception e) {
e.printStackTrace();
}
}, id);
break;
}
default: {
System.out.println("指令不存在!!!");
}
}
}
}
}