package Java版学生信息管理系统;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//添加学生
public class AddStud {
public void add(Student student) {
Connection conn = DBConnection.getConnection();
String sql = "insert into student values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, student.getXueyuan());
ps.setString(2, student.getZhuanye());
ps.setString(3, student.getGrade());
ps.setString(4, student.getBanji());
ps.setString(5, student.getName());
ps.setString(6, student.getSex());
ps.setString(7, student.getTel());
ps.setString(8, student.getJiguan());
ps.setString(9, student.getId());
ps.setString(10, student.getMath());
ps.setString(11, student.getEnglish());
ps.setString(12, student.getC_language());
ps.setString(13, student.getTeacher());
ps.execute();
System.out.println("成功添加一位学生");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
}
}
package Java版学生信息管理系统;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/db1?serverTimezone=UTC";
String UserName = "root";
String UserPassword = "root";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url,UserName,UserPassword);
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}catch (SQLException e) {
e.printStackTrace();
return null;
}
return conn;
}
public static void closeConnection(Connection conn) {
if(conn != null) {
try {
conn.close();
conn = null;
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
getConnection();
}
}
package Java版学生信息管理系统;
import java.sql.*;
public class DeleteStud {
public void Dele(String name) {
Connection conn = DBConnection.getConnection();
PreparedStatement ps;
String sql = "DELETE FROM student WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.execute();
System.out.println("该学生信息成功删除");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBConnection.closeConnection(conn);
}
}
}
package Java版学生信息管理系统;
import java.sql.*;
public class ShowStud {
public void Show(String name) {
Student student = new Student();
Connection conn = DBConnection.getConnection();
String sql = "SELECT * FROM student where name = ?";
PreparedStatement ps;
ResultSet rs;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
rs = ps.executeQuery();
if(rs.next()) {
student.setXueyuan(rs.getString("xueyuan"));
student.setZhuanye(rs.getString("zhuanye"));
student.setGrade(rs.getString("grade"));
student.setBanji(rs.getString("banji"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setTel(rs.getString("tel"));
student.setJiguan(rs.getString("jiguan"));
student.setId(rs.getString("id"));
student.setMath(rs.getString("math"));
student.setEnglish(rs.getString("english"));
student.setC_language(rs.getString("c_language"));
student.setTeacher(rs.getString("teacher"));
System.out.println("学院:"+student.getXueyuan()+
"\n专业:"+student.getZhuanye()+
"\n年级:"+student.getGrade()+
"\n班级:"+student.getBanji()+
"\n姓名:"+student.getName()+
"\n性别:"+student.getSex()+
"\n联系方式:"+student.getTel()+
"\n籍贯:"+student.getJiguan()+
"\n学号:"+student.getId()+
"\n高数:"+student.getMath()+
"\n大学英语"+student.getEnglish()+
"\nC语言程序设计"+student.getC_language()+
"\n导师:"+student.getTeacher());
}else {
System.out.println("数据错误");
}
rs.close();
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBConnection.closeConnection(conn);
}
}
}
package Java版学生信息管理系统;
public class Student {
// TODO Auto-generated method stub
private String xueyuan;
private String zhuanye;
private String grade;
private String banji;
private String name;
private String sex;
private String tel;
private String jiguan;
private String id;
private String math;
private String english;
private String c_language;
private String teacher;
public Student() {
super();
}
public Student(String xueyuan, String zhuanye, String grade, String banji, String name, String sex, String tel,
String jiguan, String id, String math, String english, String c_language, String teacher) {
super();
this.xueyuan = xueyuan;
this.zhuanye = zhuanye;
this.grade = grade;
this.banji = banji;
this.name = name;
this.sex = sex;
this.tel = tel;
this.jiguan = jiguan;
this.id = id;
this.math = math;
this.english = english;
this.c_language = c_language;
this.teacher = teacher;
}
public String getXueyuan() {
return xueyuan;
}
public void setXueyuan(String xueyuan) {
this.xueyuan = xueyuan;
}
public String getZhuanye() {
return zhuanye;
}
public void setZhuanye(String zhuanye) {
this.zhuanye = zhuanye;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getBanji() {
return banji;
}
public void setBanji(String banji) {
this.banji = banji;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getJiguan() {
return jiguan;
}
public void setJiguan(String jiguan) {
this.jiguan = jiguan;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getEnglish() {
return english;
}
public void setEnglish(String english) {
this.english = english;
}
public String getC_language() {
return c_language;
}
public void setC_language(String c_language) {
this.c_language = c_language;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
package Java版学生信息管理系统;
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Student student = new Student();
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:退出系统");
String cz = sc.next();
if(cz.equals("1")) {
//添加学生
AddStud as = new AddStud();
System.out.println("请输入学院");
student.setXueyuan(sc.next());
System.out.println("请输入专业");
student.setZhuanye(sc.next());
System.out.println("请输入年级");
student.setGrade(sc.next());
System.out.println("请输入班级");
student.setBanji(sc.next());
System.out.println("请输入姓名");
student.setName(sc.next());
System.out.println("请输入性别");
student.setSex(sc.next());
System.out.println("请输入联系方式");
student.setTel(sc.next());
System.out.println("请输入籍贯");
student.setJiguan(sc.next());
System.out.println("请输入学号");
student.setId(sc.next());
System.out.println("请输入高数成绩");
student.setMath(sc.next());
System.out.println("请输入大学英语成绩");
student.setEnglish(sc.next());
System.out.println("请输入C语言程序设计成绩");
student.setC_language(sc.next());
System.out.println("请输入导师姓名");
student.setTeacher(sc.next());
as.add(student);
}
if(cz.equals("2")) {
//查找学生
ShowStud ss = new ShowStud();
System.out.println("请输入要查询学生的姓名:");
String xm = sc.next();
ss.Show(xm);
}
if(cz.equals("3")) {
//修改学生信息,待添加
UpdateStud us = new UpdateStud();
System.out.println("请输入需要更改信息的学生姓名:");
String name = sc.next();
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("6:修改[性别]信息");
System.out.println("7:修改[联系方式]信息");
System.out.println("8:修改[籍贯]信息");
System.out.println("9:修改[学号]信息");
System.out.println("10:修改[高数]信息");
System.out.println("11:修改[大学英语]信息");
System.out.println("12:修改[C语言程序设计]信息");
System.out.println("13:修改[导师]信息");
int up = sc.nextInt();
System.out.println("请重新输入该内容");
String newer = sc.next();
us.Update(name, up, newer);
}
if(cz.equals("4")) {
//删除学生信息
DeleteStud ds = new DeleteStud();
System.out.println("请输入要删除学生的姓名:");
String name = sc.next();
ds.Dele(name);
}
if(cz.equals("5")) {
//退出系统
System.out.println("已退出系统");
System.exit(0);
}
}
}
}
package Java版学生信息管理系统;
import java.sql.*;
public class UpdateStud {
public void Update(String name, int up, String newer) {
Connection conn = DBConnection.getConnection();
PreparedStatement ps;
String sql;
if(up == 1) {
sql = "UPDATE student SET xueyuan = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 2) {
sql = "UPDATE student SET zhuanye = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 3) {
sql = "UPDATE student SET grade = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 4) {
sql = "UPDATE student SET banji = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 5) {
sql = "UPDATE student SET name = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 6) {
sql = "UPDATE student SET sex = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 7) {
sql = "UPDATE student SET tel = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 8) {
sql = "UPDATE student SET jiguan = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 9) {
sql = "UPDATE student SET id = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 10) {
sql = "UPDATE student SET math = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 11) {
sql = "UPDATE student SET english = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 12) {
sql = "UPDATE student SET c_language = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
if(up == 13) {
sql = "UPDATE student SET teacher = ? WHERE name = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, newer);
ps.setString(2, name);
ps.execute();
System.out.println("修改信息成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改信息失败");
}finally {
DBConnection.closeConnection(conn);
}
}
}
}