JAVA连接JDBC,编写简单的学生管理系统2
创建测试类
package JDBC.test;
import JDBC.entity.StudentEntity;
import JDBC.service.StudentService;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentTest {
private static StudentService studentService = new StudentService();
public static void main(String[] args) {
mainMenu();
}
public static void mainMenu(){
while (true) {
System.out.println("学生管理系统");
System.out.println("1.查询所有学生信息");
System.out.println("2.学生id查询学生信息");
System.out.println("3.新增学生信息");
System.out.println("4.学生id修改学生信息");
System.out.println("5.学生id删除学生信息");
System.out.println("6.退出系统");
Scanner scanner = new Scanner(System.in);
int result=scanner.nextInt();
switch (result){
case 1:
showAllStudent();
break;
case 2:
findByIdStudent();
break;
case 3:
insertStudent();
break;
case 4:
updateIdStudent();
break;
case 5:
delIdStudent();
break;
case 6:
System.exit(6);
}
}
}
public static void showAllStudent(){
ArrayList<StudentEntity> studentEntities=studentService.allStudent();
for (StudentEntity stu:studentEntities){
System.out.println(stu);
}
}
public static void findByIdStudent(){
System.out.println("输出学生id:");
Scanner scanner = new Scanner(System.in);
Long stuId=scanner.nextLong();
StudentEntity studentEntity=studentService.getByIdStudent(stuId);
if (studentEntity==null){
System.out.println("学生id"+ studentEntity +"不存在");
return;
}
System.out.println("学生信息为:"+studentEntity);
}
public static void delIdStudent(){
System.out.println("请输入学生id:");
Scanner scanner=new Scanner(System.in);
Long stuId =scanner.nextLong();
int result=studentService.delStudent(stuId);
if (result>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
public static void insertStudent(){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入学生id");
Long id=scanner.nextLong();
scanner.nextLine();
System.out.println("请输入学生名称");
String name=scanner.nextLine();
System.out.println("请输入学生年龄");
int age=scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生地址");
String address=scanner.nextLine();
StudentEntity studentEntity=new StudentEntity(id,name,age,address);
int result=studentService.insertStudent(studentEntity);
if (result > 0) {
System.out.println("插入学生信息成功");
} else {
System.out.println("插入学生信息失败");
}
}
public static void updateIdStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生id");
Long id=scanner.nextLong();
scanner.nextLine();
StudentEntity student=studentService.getByIdStudent(id);
if (student==null){
System.out.println("id不存在");
return;
}
System.out.println("请输入学生姓名");
String name = scanner.nextLine();
System.out.println("请输入学生年龄");
int age=scanner.nextInt();
scanner.nextLine();
System.out.println("请输入学生地址");
String address=scanner.nextLine();
StudentEntity studentEntity=new StudentEntity(id,name,age,address);
int result =studentService.updateStudent(studentEntity);
if (result > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
}
}