基于Java实现学生信息管理系统 java学生管理系统简单型

1 学生信息管理系统介绍

主要任务:方便于管理学生的各种信息

主要功能:登录、注册、验证码、删除学生、增加学生、修改学生、查看学生、导出学生信息。

思想:把每一个功能写一个类出来

使用工具:IntelliJ idea、MySQL

2 MySQL

2.1 MySQL核心思想图

2.2 启动MySQL

在windows下安装好MySQL

使用CMD运行命令,打开CMD快捷键:win+R

在cmd中执行mysql -uroot -p123456命令     注意:-p后的字符是自己MySQL的密码

2.3 创建相应的数据库

创建students数据库代码如下

create database students;

进入到students数据库中

use students;

mysql> use students;
Database changed

2.4 创建相应的表

stu数据表属性有学号(number)、姓名(name)、年龄(age)、省份(province)

建表语句为

create table stu(
 number char(10) primary key, //学号设为主键
 name varchar(6),
 age int,
 province varchar(10)
 );

user数据表序号(id)、账号(numberuser)、密码(password),提供给管理员去操作系统

建表语句为

create table user(
 id int not null auto_increment primary key, //自动生成序列号,且设为主键
 numberuser varchar(10),
 password varchar(10)
);

3 Idea

3.1 建Maven工程

点击idea中File--> New-->Project,出现下例如窗口,点击Maven

选择好jdk版本后点击Next

给项目名称取一个StudenSystem

3.2 添加依赖

打开建好的Maven工程,打开pom.xml

添加MySQL依赖      注意:更换自己MySQL的版本


 
 mysql
 mysql-connector-java
 8.0.26
 
 

3.3 添加相应的功能类

Maven中,在src-->main-->java下新创一个work包用于存放功能类

3.3.1 添加ConnJDBC类

这个类的主要功能就是封装获取连接数据库和关闭数据库,代码如下:

package work;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static java.sql.DriverManager.getConnection;
/*链接数据库*/
public class ConnJDBC {
 public static Connection getConn(){
 Connection conn=null;
 try {
 Class.forName("com.mysql.cj.jdbc.Driver");
 String url="jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
 String username="root";
 String password="123456";
 conn= getConnection(url, username, password);
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return conn;
 }
 public static void closeDB(ResultSet rs, Statement pstmt, Connection conn){
 if(rs!=null){
 try {
 rs.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 if (pstmt!=null){
 try {
 pstmt.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 if(conn!=null){
 try {
 conn.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 }
}

3.3.2 添加Enter类

最要实现登录选项页面

package work;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Enter {
 public Enter() throws SQLException, IOException {
 System.out.println("--欢迎使用只想吃软饭---学生管理系统--");
 System.out.println("-------欢迎来到登录页------");
 System.out.println("1.登录");
 System.out.println("2.注册");
 System.out.println("请选择你需要的操作:");
 Scanner sc = new Scanner(System.in);
 String select=sc.nextLine();
 switch (select){
 case "1":new Login();break;
 case "2":new Register();break;
 default: System.out.println("请输入正确的操作序号");new Enter();
 }
 }
}

3.3.3 添加NumberRepeat类

主要是判断学生的学号是否唯一

package work;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static work.ConnJDBC.getConn;
/*用来判断学号是否唯一*//*如果重复侧返回true*/
public class NumberRepeat {
 public static boolean NumberRepeats(String number) throws SQLException {
 Connection conn=getConn();
 Statement stat= conn.createStatement();
 String qsl="select *from stu";
 ResultSet rs= stat.executeQuery(qsl);
 while (rs.next()){
 String numbers= rs.getString("number");
 if (numbers.equals(number)){
 return true;
 }
 }
 return false;
 }
}

3.3.4 添加getCode类

主要生成随机的验证码

package work;
import java.util.Random;
public class getCode {
 public static String getCodes(int length) {
 String code = "";
 Random random = new Random();
 for (int i = 0; i < length; i++) {
 String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";;
 if ("char".equals(charOrNum)) {
 int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
 code = code+(char) (choice + random.nextInt(26));
 } else if ("num".equals(charOrNum)) {
 code = code+(String.valueOf(random.nextInt(10)));
 }
 }
 return code;
 }
}

3.3.5 添加Login类

主要实现登录页面,此登录与数据库中user表信息相关联,此类具有验证码功能

package work;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
import static work.getCode.getCodes;
public class Login {
 public Login() throws SQLException, IOException {
 String name;
 String password;
 String Code= getCodes(4);
 String UserCode;
 Scanner sc = new Scanner(System.in);
 while (true){
 System.out.println("-------------------------------");
 System.out.println("--欢迎使用只想吃软饭---学生管理系统--");
 System.out.println("-------------请登录-------------");
 System.out.println("请输入用户名:");
 name=sc.nextLine();
 System.out.println("请输入密码:");
 password=sc.nextLine();
 System.out.println("请输入右边的验证码: "+Code);
 UserCode=sc.nextLine();
 if (LoginCheck(name,password)&UserCode.equals(Code)){
 System.out.println("登录成功");
 new HomePage();
 }
 else if(LoginCheck(name,password)&!UserCode.equals(Code)){
 System.out.println("验证码错误,请重新登录");
 }
 else{
 System.out.println("账户或者密码错误,请重新登录");
 }}
 }
 public static boolean LoginCheck(String name,String pass) throws SQLException {
 Connection conn=getConn();
 Statement stat=conn.createStatement();
 String sql="select * from user";
 ResultSet rs= stat.executeQuery(sql);
 while (rs.next()){
 String UserName = rs.getString("numberuser");
 String UserPass = rs.getString("password");
 if (name.equals(UserName)&pass.equals(UserPass)){
 closeDB(rs,stat,conn);return true;
 }
 }
 closeDB(rs,stat,conn);
 System.out.println("mysql里面没有相关账户");
 return false;
 }
}

3.3.6 添加Register类

此类用于注册管理学生的用户

package work;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
/*注册*/
public class Register {
 public Register() throws SQLException, IOException {
 String name;
 String password;
 Scanner sc = new Scanner(System.in);
 System.out.println("---------欢迎来到注册页面---------");
 System.out.println("请输入注册的用户名:");
 name=sc.nextLine();
 /*判断账户是否存在*/
 if(CheckName(name)){
 System.out.println("请输入设定的密码:");
 password=sc.next();
 Connection conn=getConn();
 String sql = "insert into user(numberuser,password) values(?,?)";
 PreparedStatement pst=conn.prepareStatement(sql);
 pst.setString(1,name);
 pst.setString(2,password);
 int i = pst.executeUpdate();
 if (i!=0){
 System.out.println("用户添加成功,返回主页");new Enter();
 }
 else {
 System.out.println("请重新操作");new Register();
 }}else {
 System.out.println("用户已经存在,请重新操作");new Register();
 }
 }
 public static boolean CheckName(String name) throws SQLException {
 Connection conn=getConn();
 Statement stat=conn.createStatement();
 String sql="select * from user";
 ResultSet rs= stat.executeQuery(sql);
 while (rs.next()){
 String UserName = rs.getString("numberuser");
 if (name.equals(UserName)){
 ;closeDB(rs,stat,conn);return false;
 }
 }
 closeDB(rs,stat,conn);
 return true;
 }
}

3.3.7 添加HomePage类

主要实现管理系统的主页,可以选择添加、删除、修改、查看、导出学生信息与退出系统功能

package work;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
import static work.AddStudent.AddStudents;
import static work.CheckAllStudent.CheckAllStudents;
import static work.DeleteStudent.DeleteStudents;
import static work.Derive.Derives;
import static work.ModifyStudent.ModifyStudents;
public class HomePage {
 public HomePage() throws SQLException, IOException {
 System.out.println("欢迎使用只想吃软饭-学生管理系统");
 while (true) {
 System.out.println("--------学生管理系统---------");
 System.out.println("1.添加学生信息");
 System.out.println("2.删除学生信息");
 System.out.println("3.修改学生信息");
 System.out.println("4.查看学生信息");
 System.out.println("5.导出学生信息");
 System.out.println("0.退出系统");
 System.out.println("--------------------------");
 System.out.println("请选择你需要的操作:");
 Scanner sc = new Scanner(System.in);
 String select = sc.nextLine();
 switch (select) {
 case "0":{System.out.println("谢谢使用贵理工学生系统!");
 System.exit(0);break;}
 case "1": AddStudents();break;
 case "2": DeleteStudents();break;
 case "3": ModifyStudents();break;
 case "4": CheckAllStudents();break;
 case "5":Derives();break;
 default: System.out.println("请输入正确的操作序号");
 }
}}}

3.3.8 添加AddStudent类

主要实现添加学生信息功能

package work;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
import static work.NumberRepeat.NumberRepeats;
/*添加学生*//*添加成功返回true*/
public class AddStudent {
 public static void AddStudents() throws SQLException, IOException {
 System.out.println("--------欢迎来到添加学生界面-----------");
 String number,name;
 int age;
 String province;
 Scanner sc = new Scanner(System.in);
 System.out.println("请添加学生基本信息");
 System.out.println("请输入添加的学生学号:");
 number=sc.nextLine();
 //用来判断学号是否重复
 while (NumberRepeats(number)) {
 System.out.println("学号重复请重新输入学号:");
 number=sc.nextLine();
 }
 System.out.println("请输入添加的学生姓名:");
 name=sc.nextLine();
 System.out.println("请输入添加的学生年龄:");
 age=sc.nextInt();
 System.out.println("请输入添加的学生省份:");
 province=sc.next();
 System.out.println();
 Connection conn=getConn();
 String sql = "insert into stu(number,name,age,province) values(?,?,?,?)";
 PreparedStatement pst=conn.prepareStatement(sql);
 pst.setString(1,number);
 pst.setString(2,name);
 pst.setInt(3,age);
 pst.setString(4,province);
 int i = pst.executeUpdate();
 closeDB(null,pst,conn);
 if(i!=0){
 System.out.println("学生添加成功");new HomePage();
 }
 else {
 System.out.println("学生添加失败");
 }
 }
}

3.3.9 添加 DeleteStudent类

实现删除学生功能

package work;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
import static work.NumberRepeat.NumberRepeats;
/*删除学生*/
public class DeleteStudent {
 public static void DeleteStudents() throws SQLException, IOException {
 String number;
 Scanner sc = new Scanner(System.in);
 System.out.println("--------欢迎来到删除学生界面-----------");
 System.out.println("请输入要删除学生的学号:");
 number=sc.nextLine();
 /*因为学号重复返回ture,学号重复说明存在该学生,学号不重复说明没有该学生*/
 while (!NumberRepeats(number)){
 System.out.println("请重新输入存在的学号");
 number=sc.nextLine();
 }
 Connection conn=getConn();
 String sql = "delete from stu where number=?";
 PreparedStatement pst=conn.prepareStatement(sql);
 pst.setString(1,number);
 int i = pst.executeUpdate();
 closeDB(null,pst,conn);
 if (i!=0){
 System.out.println("学号为"+number+"删除成功"); new HomePage();
 }
 }
}

3.3.10 添加ModifyStudent类

实现修改学生信息,通过学号进行修改学生年龄、省份。

package work;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
import static work.NumberRepeat.NumberRepeats;
/*通过学号去修改年龄或省份*/
public class ModifyStudent {
 public static void ModifyStudents() throws SQLException, IOException {
 String select;
 String number;
 String province;
 Scanner sc = new Scanner(System.in);
 System.out.println("欢迎来到修改界面系统");
 System.out.println("请输入要修改的学生学号");
 number = sc.nextLine();
 /*因为学号重复返回ture,学号重复说明存在该学生,学号不重复说明没有该学生*/
 while (!NumberRepeats(number)) {
 System.out.println("请重新输入存在的学号");
 number = sc.nextLine();
 }
 System.out.println("1.修改学生年龄");
 System.out.println("2.修改学生省份");
 System.out.println("3.修改学生年龄和省份");
 System.out.println("4.返回上一级");
 System.out.println("--------------------------");
 System.out.println("请选择你需要的操作:");
 select = sc.nextLine();
 switch (select) {
 case "1":
 ModifyStudentOfAge(number);
 break;
 case "2":
 ModifyStudentOfProvince(number);
 break;
 case "3":
 ModifyStudentOfAll(number);
 break;
 case "4":
 new HomePage();
 break;
 default:
 System.out.println("请输入正确的操作序号");
 }
 }
 private static void ModifyStudentOfAge(String number) throws SQLException, IOException {
 int age;
 Scanner sc = new Scanner(System.in);
 System.out.println("请输入要修改的年龄:");
 age = sc.nextInt();
 Connection conn = getConn();
 String sql = "update stu set age=? where number=?";
 PreparedStatement pst = conn.prepareStatement(sql);
 pst.setInt(1, age);
 pst.setString(2, number);
 int i = pst.executeUpdate();
 if (i != 0) {
 System.out.println("修改成功");
 closeDB(null,pst,conn);
 new HomePage();
 }
 }
 private static void ModifyStudentOfProvince(String number) throws SQLException, IOException {
 String province;
 Scanner sc = new Scanner(System.in);
 System.out.println("请输入要修改的省份:");
 province = sc.nextLine();
 Connection conn = getConn();
 String sql = "update stu set province=? where number=?";
 PreparedStatement pst = conn.prepareStatement(sql);
 pst.setString(1, province);
 pst.setString(2, number);
 int i = pst.executeUpdate();
 if (i != 0) {
 System.out.println("修改成功");
 closeDB(null,pst,conn);
 new HomePage();
 }
 }
 private static void ModifyStudentOfAll(String number) throws SQLException, IOException {
 String province;
 int age;
 Scanner sc = new Scanner(System.in);
 System.out.println("请输入要修改的年龄:");
 age = sc.nextInt();
 System.out.println("请输入要修改的省份:");
 province= sc.next();
 Connection conn = getConn();
 String sql = "update stu set age=?,province=? where number=?";
 PreparedStatement pst = conn.prepareStatement(sql);
 pst.setInt(1, age);
 pst.setString(2, province);
 pst.setString(3, number);
 int i = pst.executeUpdate();
 if (i != 0) {
 System.out.println("修改成功");
 closeDB(null,pst,conn);
 new HomePage();
 }
 }
}

3.3.11 添加CheckAllStudent类

实现学生信息查看

package work;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
/*查看学生*/
public class CheckAllStudent {
 public static void CheckAllStudents() throws SQLException {
 int i=1;
 Statement stmt=null;
 ResultSet rs=null;
 Connection conn=getConn();
 stmt =conn.createStatement();
 String sql="select * from stu";
 rs= stmt.executeQuery(sql);
 System.out.println("--------所有学生信息--------");
 System.out.println("序号\t"+"\t"+"学号\t"+"\t"+"\t"+"姓名\t"+"\t"+"年龄\t"+"\t"+"省份\t");
 while (rs.next()){
 String number = rs.getString("number");
 String name = rs.getString("name");
 int age= rs.getInt("age");
 String province= rs.getString("province");
 System.out.println((i++)+"\t"+"\t"+number+"\t"+"\t"+"\t"+number+"\t"+"\t"+age+"\t"+"\t"+province);
 }
 closeDB(rs,stmt,conn);
 System.out.println("");
 }
}

3.3.12 添加Derive类

主要实现导出学生信息,这里主要导成txt文件格式

package work;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static work.ConnJDBC.closeDB;
import static work.ConnJDBC.getConn;
public class Derive {
 public static void Derives() throws IOException, SQLException {
 int i=1;
 System.out.println("--------导出学生界面-----------");
 System.out.println("导出路径为:");
 FileOutputStream out = new FileOutputStream("G:\\work\\information.txt");
 OutputStreamWriter outs = new OutputStreamWriter(out);
 int ch;
 Statement stmt=null;
 ResultSet rs=null;
 Connection conn=getConn();
 stmt =conn.createStatement();
 String sql="select * from stu";
 rs=stmt.executeQuery(sql);
 outs.write("序号\t\t学号\t\t\t姓名\t\t年龄\t\t省份\r\n");
 while (rs.next()){
 String number = rs.getString("number");
 String name = rs.getString("name");
 int age= rs.getInt("age");
 String province= rs.getString("province");
 String sum=(i++)+number+"\t\t"+name+"\t\t"+age+"\t\t"+province+"\r\n";
 outs.write(sum);
 }
 System.out.println("G:\\work\\information.txt");
 outs.close();
 out.close();
 closeDB(rs,stmt,conn);
 }
}

3.3.13 添加demo类

这个类是程序的执行入口

package work;
import java.io.IOException;
import java.sql.SQLException;
public class demo {
 public static void main(String[] args) throws SQLException, IOException {
 new Enter();
 }
 }

添加好后文件大概呈现下图

4 效果展示

望大神进行指点

作者:只想吃软饭原文地址:https://blog.csdn.net/qq_66281927/article/details/140685787

%s 个评论

要回复文章请先登录注册