Java基础—学生信息管理系统(使用数据库),供大家参考,具体内容如下
前一段时间写了学生信息管理系统(使用ArrayList),今天再来更新一下使用数据库的管理系统,总体上来说要比用ArrayList简便一些,同时输入的数据也能存储起来。
这次程序是在上一篇的基础上进行修改的,所以结构上来看十分相似。
在写程序之前,要先在MySQL上建立Student Info表。
create table Student (
ID varchar(50) primary key not null,
Name varchar(30) not null,
Gender char(5) not null,
Age int not null,
Score int not null
);
1、就和上一篇博客一样,先将学生信息进行封装,这一步并不必要,只是修改起来方便些。
public class Student {
private String stuName;
private String stuNo;
private String gender;
private int age;
private int score;
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
2.新建操作类,因为这次要使用MySql数据库,所以要使用JDBC将Java与数据库链接起来。
1> 先将mysql-connector-java.jar 加入到lib库中,之后右键Add path。
2> 然后在程序中要先开启JDBC,加载驱动器,如果驱动器不存在,会抛出异常,所以需要加上try-catch
//开启JDBC
public void getDBconn(){
String url = "jdbc:mysql://localhost:3306/homework";
String user = "root";
String password = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3>同样,在程序结束后一定要关闭连接,防止出问题。如果关闭异常,也要抛出异常。
//关闭JDBC
public void closeRs(){
try{
if(rs != null){
rs.close();
}
if(stat != null){
stat.close();
}
if(conn != null){
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
3.接下来就是程序的主体:增、删、改、查四个操作部分,这部分因为笔者偷懒,所以和上一篇使用ArrayList的结构很像,但是内容有较大的修改,每一个try-catch后都加上了finally,无论操作是否成功都要关闭连接
//增加学生信息
public void addStu(Student stu){
String sql = "insert into studentinfo values ( '" + stu.getStuNo()+ "', '" + stu.getStuName() + "', '"
+ stu.getGender() + "', " + stu.getAge() + " , " + stu.getScore() + ")";
try {
int affectedRows = stat.executeUpdate(sql);
if(affectedRows > 0){
System.out.println("该学生信息已添加!");
}else{
System.out.println("操作有误,请重新添加!");
append();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeRs();
}
}
//输入学生信息
public void append(){
Student stu = new Student();
stu.setStuName(name());
stu.setStuNo(stuNum());
stu.setGender(gender());
stu.setAge(age());
stu.setScore(score());
//将stu中的信息增添到stus中
addStu(stu);
}
//得到学生姓名
public String name(){
String name;
System.out.println("请输入学生的姓名:");
name = scanner.next();
return name;
}
//得到学生学号
public String stuNum(){
String stuNum;
System.out.println("请输入学生的学号:");
stuNum = scanner.next();
//如果学号不为11位,则重新输入,直到学号正确
if(stuNum.length() != 11){
System.out.println("您输入的学号有误!请重新输入!");
stuNum();
}
return stuNum;
}
//得到学生的性别
public String gender(){
String stuGender;
System.out.println("请输入学生的性别:");
stuGender = scanner.next();
if(!stuGender.equals("男") && !stuGender.equals("女") ){
System.out.println("您输入的学生性别有误!请重新输入!");
gender();
}
return stuGender;
}
//得到学生的年龄
public int age(){
int stuAge;
System.out.println("请输入学生的年龄:");
stuAge = scanner.nextInt();
if(stuAge < 0 || stuAge >= 80){
System.out.println("您输入的年龄有误!请重新输入!");
age();
}
return stuAge;
}
//得到学生的成绩
public int score(){
int stuScore;
System.out.println("请输入学生的成绩:");
stuScore = scanner.nextInt();
if(stuScore < 0 || stuScore > 100){
System.out.println("您输入的成绩有误!请重新输入!");
score();
}
return stuScore;
}
public void delStu(){
System.out.println("请输入您想删除的学号:");
String stuNo = scanner.next();
String sql = "delete from studentinfo where ID = '" + stuNo + "' ";
try {
int affectedRows = stat.executeUpdate(sql);
if(affectedRows > 0){
System.out.println("该学生信息已删除!");
}else{
System.out.println("操作有误,请重新输入!");
delStu();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeRs();
}
}
public void updateStu(String stuNo){
String sql = null;
information();
int choose = scanner.nextInt();
switch(choose){
case 1:
System.out.println("请输入要更新的名字:");
String Name = scanner.next();
sql = "update studentinfo set Name = '" + Name +"' where ID = '" + stuNo + "'";
break;
case 2:
System.out.println("请输入要更新的性别:");
String Gender = scanner.next();
sql = "update studentinfo set Gender = '" + Gender +"' where ID = '" + stuNo + "'";
break;
case 3:
System.out.println("请输入要更新的年龄:");
int Age = scanner.nextInt();
sql = "update studentinfo set Age = " + Age +" where ID = '" + stuNo + "'";
break;
case 4:
System.out.println("请输入要更新的成绩:");
int Score = scanner.nextInt();
sql = "update studentinfo set Score = " + Score +" where ID = '" + stuNo + "'";
break;
case 0:
System.exit(0);
break;
}
try {
int affectedRows = stat.executeUpdate(sql);
if(affectedRows > 0){
System.out.println("该学生信息已修改!");
}else{
System.out.println("操作有误!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeRs();
}
}
public void information(){
System.out.println("请选择您要修改的信息:");
System.out.println("1.姓名");
System.out.println("2.性别");
System.out.println("3.年龄");
System.out.println("4.成绩");
System.out.println("0.退出");
System.out.println("请输入序号:");
}
public void queryStuByStuNo(String stuNo){
String sql = "select * from studentinfo where ID = '" + stuNo + "'";
try {
if(stuNo.equals("0")){
sql = "select * from studentinfo";
rs = stat.executeQuery(sql);
while(rs.next()){
System.out.print("学号:" + rs.getString("ID") + "\t");
System.out.print("姓名:" + rs.getString("Name") + "\t");
System.out.print("性别:" + rs.getString("Gender") + "\t");
System.out.print("年龄:" + rs.getInt("Age") + "\t");
System.out.println("成绩:" + rs.getInt("Score") + "\t");
}
}else{
if(rs.next()){
rs = stat.executeQuery(sql);
System.out.print("学号:" + rs.getString("ID") + "\t");
System.out.print("姓名:" + rs.getString("Name") + "\t");
System.out.print("性别:" + rs.getString("Gender") + "\t");
System.out.print("年龄:" + rs.getInt("Age") + "\t");
System.out.println("成绩:" + rs.getInt("Score") + "\t");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeRs();
}
}
4.定义操作菜单,允许用户输入选择要操作的功能。
import java.util.Scanner;
public class SystemService {
Scanner scanner = new Scanner(System.in);
StudentSystem ss = new StudentSystem();
public void serviceChoose(){
System.out.println("-----欢迎使用学生信息管理系统-----");
while(true){
ss.getDBconn();
System.out.println("1.添加学生信息");
System.out.println("2.删除学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.查询学生信息");
System.out.println("0.退出系统");
System.out.println("请输入序号:");
switch(scanner.nextInt()){
case 1:
ss.append();
System.out.println();
break;
case 2:
ss.delStu();
System.out.println();
break;
case 3:
System.out.println("请输入要修改的学生的学号:");
ss.updateStu(scanner.next());
System.out.println();
break;
case 4:
System.out.println("请输入要查询的学生的学号(若为0,则查询所有学生信息):");
ss.queryStuByStuNo(scanner.next());
System.out.println();
break;
case 0:
System.exit(0);//退出系统
ss.closeRs();
break;
}
}
}
}
5.定义主方法,使用该系统
public class Main {
public static void main(String[] args) {
SystemService ss = new SystemService();
ss.serviceChoose();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。