Java 学生管理系统(使用集合+JDBC)

  最近学习Java集合和JDBC连接数据库,写了一个小的学生管理系统,记录一下~~ 只实现了student表的增删改查功能,当然可以根据个人需求进行扩展其他功能

1.数据库

创建了一个student表:

 

 

 

 

 2.代码结构

我使用的是idea,只看我方框里的文件就可以了

db.properties是连接数据库的配置文件,根据自己的数据库名字和表名进行修改就好,记得file文件夹要标记为source root目录

lib文件夹放的是引入的jar包,这里需要引入mysql-connector-java jar包,文件夹需要标记为library

src是程序代码,可以按照我创建的方式建立文件夹和类

 

3.代码

1、db.properties

url=jdbc:mysql://localhost:3306/jdbc_study?useSSL=false&characterEncoding=utf8
username=root
password='改为自己的密码'
driver=com.mysql.jdbc.Driver
jdbc_study是我的数据库名字,你可以修改为自己的名字

 2、Student.java

package com.stuManagement.model;

import java.util.Objects;

public class Student {
    private int id;
    private String stuId;
    private String name;
    private int age;

    public Student() {
    }

    public Student(int id, String stuId, String name, int age) {
        this.id = id;
        this.stuId = stuId;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", stuId='" + stuId + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}'+ "\n";
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

ApplicationService.java

package com.stuManagement.service;

import com.stuManagement.model.Student;
import com.stuManagement.util.DBUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class ApplicationService {
    private static Connection connection;
    private static PreparedStatement preparedStatement;
    private static ResultSet resultSet;


    public static ArrayList<Student> selectAll() {
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from student";
            ArrayList<Student> students = new ArrayList<>();
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                students.add(new Student(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getInt(4)));
            }
//            System.out.println("students = " + students);
            return students;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DBUtils.close(connection, preparedStatement, resultSet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static Student selectByStuID(String stuId) {
        try {
            connection = DBUtils.getConnection();
            String sql = "select * from student where stuId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, stuId);
            resultSet = preparedStatement.executeQuery();
            // next()就像指针一样,你要把指针从resultSet壳上移到里面的内容上!不next就报错!
            resultSet.next();
            Student student = new Student(resultSet.getInt(1), resultSet.getString(2), resultSet.getString(3), resultSet.getInt(4));
            return student;
//            System.out.println("student = " + student);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                DBUtils.close(connection, preparedStatement, resultSet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String add(String stuId,String name,int age){
        try {
            connection = DBUtils.getConnection();
            String sql = "insert into student(stuId,name,age) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,stuId);
            preparedStatement.setString(2,name);
            preparedStatement.setInt(3,age);
            int i = preparedStatement.executeUpdate();
            if(i>0){
                return "添加成功!";
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                DBUtils.close(connection,preparedStatement);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "添加失败!";
    }

    public static String deleteByStuId(String stuId){
        try {
            connection = DBUtils.getConnection();
            String sql = "delete from student where stuId = ?;";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,stuId);
            int i = preparedStatement.executeUpdate();
            if (i>0){
                return "删除成功";
            }

        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                DBUtils.close(connection,preparedStatement);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "删除失败!";
    }

    public static String updateByStuId(String stuId, String name,int age){
        try {
            connection = DBUtils.getConnection();
            String sql = "update student set name = ?,age = ? where stuId = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setInt(2,age);
            preparedStatement.setString(3,stuId);

            int i = preparedStatement.executeUpdate();
            if(i>0){
                return "修改成功!";
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                DBUtils.close(connection,preparedStatement);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "修改成功!";
    }
}

DBUtils.java

package com.stuManagement.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class DBUtils {
    private static String url;
    private static String username;
    private static String password;
    private static String driver;

    static {
        try {
            InputStream stream = ClassLoader.getSystemResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(stream);

            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");

        } catch (Exception e){
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws Exception {
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }
    public static void close(Connection connection,PreparedStatement preparedStatement) throws Exception{
        if(preparedStatement!=null){
            preparedStatement.close();
        }
        if (connection!=null){
            connection.close();
        }
    }
    public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet) throws Exception{
        if(resultSet!=null){
            resultSet.close();
        }
        if(preparedStatement!=null){
            preparedStatement.close();
        }
        if (connection!=null){
            connection.close();
        }
    }
}

RunApplication.java

package com.stuManagement;

import com.stuManagement.service.ApplicationService;
import java.util.Scanner;

public class RunApplication {
    public static Scanner scanner = new Scanner(System.in);
    public static String stuId;
    public static String name;
    public static int age;

    public static void main(String[] args) {
        init();
    }

    public static void init() {
        System.out.println("---------welcome to student management system !-----------");
        System.out.println("请选择功能(输入序号):\n" + "1.查询所有学生信息\n" + "2.通过学号查找学生\n" + "3.添加新的学生信息\n" + "4.修改指定学号的学生信息\n" + "5.删除指定学号的信息\n" + "6.退出系统\n");
        option();
    }

    public static void option() {
        switch (scanner.nextInt()) {
            case 1:
                System.out.println(ApplicationService.selectAll());
                init();
                break;
            case 2:
                System.out.println("请输入学生学号:");
                System.out.println(ApplicationService.selectByStuID(scanner.next()));
                init();
                break;
            case 3:
                System.out.println("学号:");
                stuId = scanner.next();
                System.out.println("姓名:");
                name = scanner.next();
                System.out.println("年龄:");
                age = scanner.nextInt();
                System.out.println(ApplicationService.add(stuId, name, age));
                init();
                break;
            case 4:
                System.out.println("请输入需要修改信息的学生学号:");
                stuId = scanner.next();
                System.out.println("修改姓名为:");
                name = scanner.next();
                System.out.println("修改年龄为:");
                age = scanner.nextInt();
                System.out.println(ApplicationService.updateByStuId(stuId, name, age));
                init();
                break;
            case 5:
                System.out.println("请输入学生学号:");
                System.out.println(ApplicationService.deleteByStuId(scanner.next()));
                init();
                break;
            case 6:
                System.exit(0);
                init();
                break;
            default:
                System.out.println("输入不合法,请重新输入序号!");
                init();
                break;
        }
    }
}

4.结果

代码直接复制就好,有什么问题也可以给我留言~~

 

 

 

posted @ 2021-02-14 16:37  yyComeOn  阅读(576)  评论(0编辑  收藏  举报