JAVA连接JDBC,编写简单的学生管理系统2

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("修改失败");
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值