jdbc-学生管理系统

学生管理系统

java + 数据库实现(具体内容包括:数组,列表,线程,字符串,数据库)

实体类模块包括

学生学号,姓名,课程数,成绩…
数据库结构
在这里插入图片描述

具体目录信息
在这里插入图片描述

package ccu.practice.student_systeminfo.bean;

import java.io.Serializable;

//学生管理系统

public class Student_info implements Serializable {//实体类
    private static final long serialVersionUID=1l;//增加兼容的序列化
    private String student_no;//学号
    private String student_name;//姓名
    private int class_number;//课程门数
    private String python_score;//python成绩
    private String Ideological_scoree;//思政
    private String pr_score;//体育成绩
    private String math_score;
    private String set_dream_score;
    private String web_connect_score;
    private String linux_score;
    private String form_score;
    public Student_info() {
    }
    public Student_info(String student_no, String student_name, int class_number, String python_score, String ideological_scoree, String pr_score, String math_score, String set_dream_score, String web_connect_score, String linux_score, String form_score) {
        this.student_no = student_no;
        this.student_name = student_name;
        this.class_number = class_number;
        this.python_score = python_score;
        Ideological_scoree = ideological_scoree;
        this.pr_score = pr_score;
        this.math_score = math_score;
        this.set_dream_score = set_dream_score;
        this.web_connect_score = web_connect_score;
        this.linux_score = linux_score;
        this.form_score = form_score;
    }

    public String getStudent_no() {
        return student_no;
    }

    public void setStudent_no(String student_no) {
        this.student_no = student_no;
    }

    public String getStudent_name() {
        return student_name;
    }

    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }

    public int getClass_number() {
        return class_number;
    }

    public void setClass_number(int class_number) {
        this.class_number = class_number;
    }

    public String getPython_score() {
        return python_score;
    }

    public void setPython_score(String python_score) {
        this.python_score = python_score;
    }

    public String getIdeological_scoree() {
        return Ideological_scoree;
    }

    public void setIdeological_scoree(String ideological_scoree) {
        Ideological_scoree = ideological_scoree;
    }

    public String getPr_score() {
        return pr_score;
    }

    public void setPr_score(String pr_score) {
        this.pr_score = pr_score;
    }

    public String getMath_score() {
        return math_score;
    }

    public void setMath_score(String math_score) {
        this.math_score = math_score;
    }

    public String getSet_dream_score() {
        return set_dream_score;
    }

    public void setSet_dream_score(String set_dream_score) {
        this.set_dream_score = set_dream_score;
    }

    public String getWeb_connect_score() {
        return web_connect_score;
    }

    public void setWeb_connect_score(String web_connect_score) {
        this.web_connect_score = web_connect_score;
    }

    public String getLinux_score() {
        return linux_score;
    }

    public void setLinux_score(String linux_score) {
        this.linux_score = linux_score;
    }

    public String getForm_score() {
        return form_score;
    }
    public void setForm_score(String form_score) {
        this.form_score = form_score;
    }
}

数据库连接模块:常用数据库连接+数据库语句实现方法

package ccu.practice.student_systeminfo.dao;
import java.sql.*;
public class DButl {
    public static Connection getconnetcion(){//数据库建立模块
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_systeminfo?useUnicode(true)&characterEncoding=utf-8", "root", "200062");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){
        try {//关闭模块
            if (resultSet!=null){
                resultSet.close();
                resultSet = null;
            }
            if (preparedStatement!=null){
                preparedStatement.close();
                preparedStatement = null;
            }
            if (connection!=null){
                connection.close();
                connection = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static int executeUpdate(String sql,Object []objects ){
        int rows=0;//建立一个判断标识
        Connection connection = getconnetcion();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);//建立管道连接进行sql语句的条件筛选
            if (objects!=null){//判断数组中是否铀元素
                for (int i=0;i<objects.length;i++){
                    preparedStatement.setObject(i+1,objects[i]);//将每一行数据放入数组
                }
            }
            rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows ;
    }
}

增删改查方法

package ccu.practice.student_systeminfo.dao;

import ccu.practice.student_systeminfo.bean.Student_info;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class Student_infoDao {
    public static Connection connection;
    public static ResultSet resultSet;
    public static PreparedStatement preparedStatement;
    public String sql;
    public static Scanner input = new Scanner(System.in);
    public int studentinfo_add(Student_info student_info){//学生增加
        int rows = 0;
        sql = "insert into student_info(student_no,student_name,class_number,python_score,Ideological_scoree,pr_score,math_score,set_dream_score,web_connect_score,linux_score,form_score) values(?,?,?,?,?,?,?,?,?,?,?)";
        Object [] objects = {student_info.getStudent_no(),
                student_info.getStudent_name(),
                student_info.getClass_number(),
                student_info.getPython_score(),
                student_info.getIdeological_scoree(),
                student_info.getPr_score(),
                student_info.getMath_score(),
                student_info.getSet_dream_score(),
                student_info.getWeb_connect_score(),
                student_info.getLinux_score(),
                student_info.getForm_score()};
        rows = DButl.executeUpdate(sql,objects);
        return rows;
    }
    public Student_info studentinfo_look(String student_no){//学生查找
        Student_info student_info = null;
        sql = "select * from student_info where student_no=?";
        connection = DButl.getconnetcion();
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                student_info = new Student_info(resultSet.getString(1),
                        resultSet.getString(2),
                        resultSet.getInt(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getString(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getString(10),
                        resultSet.getString(11));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButl.close(resultSet,preparedStatement,connection);
        }
        return student_info;
    }
    public int delete(String student_no){
        int rows = 0;
        connection = DButl.getconnetcion();
        String sql = "delete from student_info where student_no=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,student_no);
            rows = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           DButl.close(resultSet,preparedStatement,connection);
        }
        return rows;
    }
//   public  int delete(Student_info student_info){//学生删除
//       int rows = 0;
//       sql = "delete from student_info where student_no=?";
//        connection = DButl.getconnetcion();
//            Object [] objects = {student_info.getStudent_no(),
//                    student_info.getStudent_name(),
//                    student_info.getClass_number(),
//                    student_info.getPython_score(),
//                    student_info.getIdeological_scoree(),
//                    student_info.getPr_score(),
//                    student_info.getMath_score(),
//                    student_info.getSet_dream_score(),
//                    student_info.getWeb_connect_score(),
//                    student_info.getLinux_score(),
//                    student_info.getForm_score()};
//           rows = DButl.executeUpdate(sql,objects);
//        return rows;
//    }
    public void select(int student_no) {//学生显示
    }
    public int update(Student_info student_info){
        int rows = 0;
        connection = DButl.getconnetcion();
        String sql = "update student_info set student_no=?,student_name=''?'',class_number=?,python_score=?,Ideological_scoree=?,pr_score=?,math_score=?,set_dream_score=?,web_connect_score=?,linux_score=?,form_score=? where student_no=?";
        Object [] objects = {student_info.getStudent_no(),
                student_info.getStudent_name(),
                student_info.getClass_number(),
                student_info.getPython_score(),
                student_info.getIdeological_scoree(),
                student_info.getPr_score(),
                student_info.getMath_score(),
                student_info.getSet_dream_score(),
                student_info.getWeb_connect_score(),
                student_info.getLinux_score(),
                student_info.getForm_score(),
                student_info.getStudent_no()
        };
        rows = DButl.executeUpdate(sql,objects);
        return rows;
    }
}

对方法进行测试

package ccu.practice.student_systeminfo.test;

import ccu.practice.student_systeminfo.bean.Student_info;
import ccu.practice.student_systeminfo.dao.Student_infoDao;

import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;

public class StudentTest {
    public static Scanner input = new Scanner(System.in);
    public static void studentinfo_add(){ //增加
        System.out.println("请输入学号:");
        String student_no = input.next();
        System.out.println("请输入姓名:");
        String student_name = input.next();
        System.out.println("请输入总科目数:");
        int class_number = input.nextInt();
        System.out.println("请输入python成绩:");
        String python_score = input.next();
        System.out.println("请输入思政成绩:");
        String ideological_score = input.next();
        System.out.println("请输入体育成绩:");
        String pr_score = input.next();
        System.out.println("请输入数学成绩:");
        String math_score = input.next();
        System.out.println("请输入筑梦成绩:");
        String set_dream_score =  input.next();
        System.out.println("请输入网络互联成绩:");
        String web_connect_score = input.next();
        System.out.println("请输入linux成绩:");
        String linux_score = input.next();
        System.out.println("请输入形式与政策成绩:");
        String form_score = input.next();
        Student_info student_info = new Student_info(student_no,
                student_name,
                class_number,
                python_score,
                ideological_score,
                pr_score,
                math_score,
                set_dream_score,
                web_connect_score,
                linux_score,
                form_score);
        Student_infoDao student_infoDao = new Student_infoDao();
        int rows = student_infoDao.studentinfo_add(student_info);
        if (rows>0){
            System.out.println("插入成功");
        }
    }
    public static void studentinfo_look(){//查找
        System.out.println("请输入需查找学生的学号");
        String student_no = input.next();
        Student_infoDao student_infoDao = new Student_infoDao();
        Student_info student_info = student_infoDao.studentinfo_look(student_no);
        if (student_info==null){
            System.out.println("未查找的该学生");
        }else{
            System.out.println("该学生已存在");
        }
    }
//    public static void delete() {//删除
//        System.out.println("请输入需删除的学生的学号");
//        String student_no = input.next();
//        Student_info student_info = new Student_info();
//        //student_info.setStudent_no(student_no);
//        Student_infoDao student_infoDao = new Student_infoDao();
//        int rows = student_infoDao.delete(student_no);
//        if (rows>0){
//           System.out.println("删除成功");
//        }else{
//             System.out.println("删除失败");
//        }
//    }
    public static void delete() {//删除
        System.out.println("请输入需删除的学生的学号");
        String student_no = input.next();
        Student_info student_info = new Student_info();
        //student_info.setStudent_no(student_no);
        Student_infoDao student_infoDao = new Student_infoDao();
        int rows = student_infoDao.delete(student_no);
        if (rows>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }
    public static void select() {//信息显示

    }
    public static void update(){//改
            System.out.println("请输入学号:");
            String student_no = input.next();
            System.out.println("请输入姓名:");
            String student_name = input.next();
            System.out.println("请输入总科目数:");
            int class_number = input.nextInt();
            System.out.println("请输入python成绩:");
            String python_score = input.next();
            System.out.println("请输入思政成绩:");
            String ideological_score = input.next();
            System.out.println("请输入体育成绩:");
            String pr_score = input.next();
            System.out.println("请输入数学成绩:");
            String math_score = input.next();
            System.out.println("请输入筑梦成绩:");
            String set_dream_score =  input.next();
            System.out.println("请输入网络互联成绩:");
            String web_connect_score = input.next();
            System.out.println("请输入linux成绩:");
            String linux_score = input.next();
            System.out.println("请输入形式与政策成绩:");
            String form_score = input.next();
            Student_info student_info = new Student_info(student_no,
                    student_name,
                    class_number,
                    python_score,
                    ideological_score,
                    pr_score,
                    math_score,
                    set_dream_score,
                    web_connect_score,
                    linux_score,
                    form_score);
            Student_infoDao student_infoDao = new Student_infoDao();
            int rows = student_infoDao.update(student_info);
            if (rows>0){
                System.out.println("更新成功");
            }
        }
    }

模块主函数程序使用

package ccu.practice.student_systeminfo.test;

import java.util.Scanner;

public class Test {
    public static Scanner input = new Scanner(System.in);
    public static void menu(){
            try {
                System.out.println("************************************");
                System.out.println("*************学生管理系统*************");
                System.out.println("              1.学生信息添加           ");
                Thread.sleep(500);
                System.out.println("              2.学生信息查找           ");
                Thread.sleep(500);
                System.out.println("              3.学生信息删除           ");
                Thread.sleep(500);
                System.out.println("              4.学生信息修改           ");
                Thread.sleep(500);
                System.out.println("              5.学生信息显示           ");
                Thread.sleep(500);
                System.out.println("              6.退出程序           ");
                Thread.sleep(500);
                System.out.println("              0.返回主菜单             ");
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("请输入你的选项:");
            String choose = input.next();
            switch (choose){
                case "1":
                    StudentTest.studentinfo_add();
                    returnEMENU();
                    break;
                case"2":
                    StudentTest.studentinfo_look();
                    returnEMENU();
                    break;
                case"3":
                    StudentTest.delete();
                    returnEMENU();
                    break;
                case "4":
                    StudentTest.update();
                    returnEMENU();
                    break;
                case "5":
                    StudentTest.select();
                    returnEMENU();
                    break;
                case"6":
                    System.exit(6);
                    System.out.println("退出程序");
                default:
                    System.out.println("输入错误");
            }
        }
        public static  void returnEMENU(){
            System.out.println("按0返回主菜单");
            String choose = input.next();
        if (choose.equals("0")){
            menu();
        }
        }
    public static void main(String[] args) {
        menu();
    }
}

注:数据库的名字及连接服务需要自己更新,此系统为作者的数据库的部分内容。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值