建立 连接数据库的工具类
import java.sql.Connection;
import java.sql.DriverManager;
public class JdbcMysql {
//四个参数
static String userName="root";//数据库 用户名
static String passWord="root";//数据库 密码
static String url="jdbc:mysql://localhost:3306/keshe";//keshe 为数据库名
static String driver="com.mysql.jdbc.Driver";
//工具方法 获得数据库的连接
public static Connection getconnection() throws Exception{
Connection connection = null;
try {
//加载驱动
Class.forName(driver);
//通过 用户名 密码 url 连接数据库
connection=DriverManager.getConnection(url, userName, passWord);
} catch (Exception e) {
e.printStackTrace();
throw e;//向上抛出异常
}
return connection;
}
}
建立一个学生类 用来存放从数据库读取到的学生信息
public class student {
private int student_id;
private String student_name;
private String sex;
private int age;
private String major;
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "student [student_id=" + student_id + ", student_name=" + student_name + ", sex=" + sex + ", age=" + age
+ ", major=" + major + "]";
}
}
建立一个课程类用来存放 从数据库读取到的课程的信息
public class course {
private int course_id;
private String course_name;
private double course_time;
private double course_grade;
private int teacher_id;
public int getCourse_id() {
return course_id;
}
public void setCourse_id(int course_id) {
this.course_id = course_id;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public double getCourse_time() {
return course_time;
}
public void setCourse_time(double course_time) {
this.course_time = course_time;
}
public double getCourse_grade() {
return course_grade;
}
public void setCourse_grade(double course_grade) {
this.course_grade = course_grade;
}
public int getTeacher_id() {
return teacher_id;
}
public void setTeacher_id(int teacher_id) {
this.teacher_id = teacher_id;
}
@Override
public String toString() {
return "course [course_id=" + course_id + ", course_name=" + course_name + ", course_time=" + course_time
+ ", course_grade=" + course_grade + ", teacher_id=" + teacher_id + "]";
}
}
建立一个 用户类 (用户有三种 学生 老师 管理员 他们的权限不同)用来存放从数据库读取到的用户信息
public class User {
private String userName;
private String power;
private String userPasswd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPower() {
return power;
}
public void setPower(String power) {
this.power = power;
}
public String getUserPasswd() {
return userPasswd;
}
public void setUserPasswd(String userPasswd) {
this.userPasswd = userPasswd;
}
@Override
public String toString() {
return "User [userName=" + userName + ", power=" + power + ", userPasswd=" + userPasswd + "]";
}
}
建立一个studentdao 类 用来存放操作数据库的各种方法(偷懒没有写成绩的查询方法)
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class studentdao {
//获取登录用户
public static List<User> getuser() {
List<User> list = new ArrayList<User>();
//1、获得数据库连接
//2、编写sql
//3、执行sql
//4、获得结果
//5、根据结果解析
try {
Connection connection=JdbcMysql.getconnection();
String sql=" SELECT * FROM user";
//执行sql对象
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);//执行查询操作 result接收查询结果
while(result.next()){
//判断里面存在值
User user=new User();
user.setUserName(result.getString("user_name"));
user.setUserPasswd(result.getString("user_passwd"));
user.setPower(result.getString("user_identity"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//查询单个学生
public static void getstudent(int id) {
List<student> list = new ArrayList<student>();
//1、获得数据库连接
//2、编写sql
//3、执行sql
//4、获得结果
//5、根据结果解析
try {
Connection connection=JdbcMysql.getconnection();
String sql =" SELECT * FROM student WHERE student_id="+id;
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);//执行查询操作 result接收查询结果
while(result.next()) {
student s = new student();
s.setStudent_id(result.getInt("student_id"));
s.setStudent_name(result.getString("student_name"));
s.setSex(result.getString("sex"));
s.setAge(result.getInt("age"));
s.setMajor(result.getString("major"));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
for (student student : list) {
System.out.println(student);
}
}
//查询所有的学生信息
public static void getstudentAll() {
List<student> list = new ArrayList<student>();
//1、获得数据库连接
//2、编写sql
//3、执行sql
//4、获得结果
//5、根据结果解析
try {
Connection connection=JdbcMysql.getconnection();
String sql=" SELECT * FROM student";
//执行sql对象
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);//执行查询操作 result接收查询结果
while(result.next()){
//判断里面存在值
student s=new student();
s.setStudent_id(result.getInt("student_id"));
s.setStudent_name(result.getString("student_name"));
s.setSex(result.getString("sex"));
s.setAge(result.getInt("age"));
s.setMajor(result.getString("major"));
list.add(s);
}
} catch (Exception e) {
e.printStackTrace();
}
for (student student : list) {
System.out.println(student);
}
}
//查询课程信息
public void getcourseAll() {
List<course> list = new ArrayList<course>();
//1、获得数据库连接
//2、编写sql
//3、执行sql
//4、获得结果
//5、根据结果解析
try {
Connection connection=JdbcMysql.getconnection();
String sql=" SELECT * FROM course";
//执行sql对象
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);//执行查询操作 result接收查询结果
while(result.next()){
//判断里面存在值
course c=new course();
c.setCourse_id(result.getInt("course_id"));
c.setCourse_name(result.getString("course_name"));
c.setCourse_time(result.getDouble("course_time"));
c.setCourse_grade(result.getDouble("course_grade"));
c.setTeacher_id(result.getInt("teacher_id"));
list.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
for (course course : list) {
System.out.println(course);
}
}
// 增加学生
public void insertstudent (student s) {
try {
Connection connection = JdbcMysql.getconnection();
// ? 占位符 在后面给他赋值
String sql="INSERT INTO student VALUES(?,?,?,?,?)";
java.sql.PreparedStatement ps= connection.prepareStatement(sql);
ps.setInt(1, s.getStudent_id());
ps.setString(2, s.getStudent_name());
ps.setString(3, s.getSex());
ps.setInt(4, s.getAge());
ps.setString(5, s.getMajor());
//执行sql
ps.executeUpdate();// 增加 修改 删除
} catch (Exception e) {
e.printStackTrace();
}
}
//删除学生
public void deletestudent(int i) {
try {
Connection connection=JdbcMysql.getconnection();
String sql="DELETE FROM student WHERE student_id=?";
java.sql.PreparedStatement ps= connection.prepareStatement(sql);
ps.setInt(1, i);
ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//修改学生成绩
public void updateGrade(int score, int student_id,int course_id) {
try {
Connection connection = JdbcMysql.getconnection();
String sql = "UPDATE score SET grade=? WHERE student_id=? AND course_id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, score);
ps.setInt(2, student_id);
ps.setInt(3, course_id);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
}
//增加课程信息
public void insertcourse(course c) {
try {
Connection connection = JdbcMysql.getconnection();
String sql = "INSERT INTO course VALUES(?,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1,c.getCourse_id() );
ps.setString(2, c.getCourse_name());
ps.setDouble(3, c.getCourse_time());
ps.setDouble(4, c.getCourse_grade());
ps.setInt(5, c.getTeacher_id());
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
}
//删除课程信息
public void deletecourse(int id) {
try {
Connection connection = JdbcMysql.getconnection();
String sql = "DELETE FROM course WHERE course_id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
}
//修改课程的老师
public void updateCourse(int teacher_id,int course_id) {
try {
Connection connection = JdbcMysql.getconnection();
String sql = "UPDATE course SET teacher_id=? WHERE course_id=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, teacher_id);
ps.setInt(2, course_id);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}
}
}
main方法
public class test {
public static void main(String[] args) {
Login();//调用login方法
}
//创建login方法
public static void Login() {
int count = 0 ;
studentdao sd = new studentdao();
List<User> list = sd.getuser();
System.out.println("欢迎来到学生管理系统");
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的用户名");
String Username = sc.nextLine();
System.out.println("请出入你的密码");
String userPasswd = sc.nextLine();
for (User user : list) {
if(Username.equals(user.getUserName())) {
if( userPasswd.equals(user.getUserPasswd()) ) {
System.out.println("恭喜你登陆成功");
if(user.getPower().equals("学生")) {
while(true) {
System.out.println("请选择你的操作");
System.out.println("1、查询所有学生信息");
System.out.println("2、 根据id查找学生信息");
System.out.println("3、 退出");
int a = sc.nextInt();
sc.nextLine();
if (a == 1) {
sd.getstudentAll();
}
else if (a == 3) {
break;
}
else if (a ==2) {
System.out.println("请输入你要查询的学生id");
int n = sc.nextInt();
sc.nextLine();
sd.getstudent(n);
System.out.println("查询成功");
}
}
}
else if(user.getPower().equals("老师")) {
while(true) {
System.out.println("请选择你的操作");
System.out.println("1、查询学生信息");
System.out.println("2、根据学生id查询学生信息");
System.out.println("3、查询课程信息");
System.out.println("4、 退出");
int a = sc.nextInt();
sc.nextLine();
if( a == 1) {
sd.getstudentAll();
}
else if (a == 3) {
sd.getcourseAll();
}
else if (a == 4) {
break;
}
else if (a == 2) {
System.out.println("请输入你要查询的学生id");
int n = sc.nextInt();
sc.nextLine();
sd.getstudent(n);
System.out.println("查询成功");
}
}
}
else if(user.getPower().equals("管理员")) {
while(true) {
System.out.println("请选择你的操作");
System.out.println("1、查询学生信息");
System.out.println("2、根据id查找学生信息");
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、 退出");
int a = sc.nextInt();
sc.nextLine();
if(a == 1) {
sd.getstudentAll();
}
else if (a == 3) {
student s = new student();
System.out.println("请输入学生id");
int id = sc.nextInt();
sc.nextLine();
System.out.println("请输入学生姓名");
String name = sc.nextLine();
System.out.println("请输入学生性别");
String sex = sc.nextLine();
System.out.println("请输入学生年龄");
int age = sc.nextInt();
sc.nextLine();
System.out.println("请输入学生专业");
String major = sc.nextLine();
s.setStudent_id(id);
s.setStudent_name(name);
s.setSex(sex);
s.setAge(age);
s.setMajor(major);
sd.insertstudent(s);
System.out.println("添加成功");
}
else if (a == 4) {
System.out.println("请输入删除学生的id");
int id = sc.nextInt();
sc.nextLine();
sd.deletestudent(id);
System.out.println("删除成功");
}
else if (a == 5){
System.out.println("请输入你要修改的学成id");
int student_id = sc.nextInt();
sc.nextLine();
System.out.println("请输入你要修改的课程id");
int course_id = sc.nextInt();
sc.nextLine();
System.out.println("请输入你要把成绩修改为多少");
int score = sc.nextInt();
sd.updateGrade(score, student_id, course_id);
System.out.println("修改成功");
}
else if (a == 6) {
sd.getcourseAll();
}
else if (a == 7) {
course c = new course();
System.out.println("请输入你要增加的课程的id");
int course_id = sc.nextInt();
sc.nextLine();
System.out.println("请输入你要增加的课程的名称");
String course_name = sc.nextLine();
System.out.println("请输入你要增加课程的课时");
double time = sc.nextDouble();
System.out.println("请输入你要增加课程的学分");
double grade = sc.nextDouble();
System.out.println("请输入你要增加的课程的老师id");
int teacher_id = sc.nextInt();
sc.nextLine();
c.setTeacher_id(course_id);
c.setCourse_name(course_name);
c.setCourse_time(time);
c.setCourse_grade(grade);
c.setTeacher_id(teacher_id);
sd.insertcourse(c);
System.out.println("增加成功");
}
else if (a == 8) {
System.out.println("请输入你要删除的课程id");
int id = sc.nextInt();
sc.nextLine();
sd.deletecourse(id);
System.out.println("删除成功");
}
else if (a == 9) {
System.out.println("请输入你要更改的课程的id");
int coures_id = sc.nextInt();
sc.nextLine();
System.out.println("请输入你要把改 课程改为id为多少的老师");
int teacher_id =sc.nextInt();
sc.nextLine();
sd.updateCourse(teacher_id, coures_id);
System.out.println("修改成功");
}
else if (a == 10) {
break;
}
else if (a == 2) {
System.out.println("请输入你要查询的学生id");
int n = sc.nextInt();
sc.nextLine();
sd.getstudent(n);
System.out.println("查询成功");
}
}
}
break;
}
else {
System.out.println("密码错误");
break;
}
}
count++;
if(count == list.size()) {
System.out.println("改用户名不存在");
}
}
}
}
}