简单的学生选课模拟系统(java)

本例主要练习jdbc技术以及sql语句对数据库进行增删改查的操作

四张表

简单的学生选课模拟系统(java)_第1张图片
grade(班级表)
简单的学生选课模拟系统(java)_第2张图片表中数据
简单的学生选课模拟系统(java)_第3张图片

result(成绩表)
简单的学生选课模拟系统(java)_第4张图片表中数据
简单的学生选课模拟系统(java)_第5张图片

student(学生表)
简单的学生选课模拟系统(java)_第6张图片表中数据
简单的学生选课模拟系统(java)_第7张图片

subject(科目表)
简单的学生选课模拟系统(java)_第8张图片简单的学生选课模拟系统(java)_第9张图片

java代码实现
Util.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Util {
	Connection conn =null;
	Statement st = null;
	Scanner input = new Scanner(System.in);
	//建立连接
	public void getConn() throws Exception {
		//加载驱动
		Class.forName("com.mysql.cj.jdbc.Driver");
		
		//连接数据库
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java27?characterEncoding=utf-8&serverTimezone=GMT%2B8",
				"root","root");
	}
	//关闭连接
	public void closeConn() throws Exception {
		conn.close();
		if(st!=null) {
			st.close();
		}
		
	}
	//选课系统
	public void courseSystem() throws Exception {
		int isLoop = 1;
		System.out.println("欢迎进入学生选课系统!");
		do {
			System.out.println("请输入学号:");
			int studentNo = input.nextInt();
			System.out.println("请输入密码:");
			String pwd = input.next();
			//判断学生是否存在
			if(login(studentNo,pwd)) {
				isLoop=4;
				boolean isContinue = false;
				do {
					showData();
					int choice = input.nextInt();
					switch (choice) {
					case 1:
						subjectInfo();
						break;
					case 2:
						selectCourse(studentNo);
						break;
					case 3:
						subjectInfo();
						addCourse(studentNo);
						break;
					case 4:
						lookResult(studentNo);
						break;
					case 5:
						gradeInfo(studentNo);
						break;
					case 6:
						classmates(studentNo);
						break;
					case 7:
						selectCourse(studentNo);
						deleteCourse(studentNo);
						break;
					case 8:
						updatePwd(studentNo);
						
						break;
					default:
						break;
					}
					System.out.println("请输入0继续1退出");
					int a = input.nextInt();
					if(a==0) {
						isContinue=true;
					}else {
						System.out.println("系统已退出");
						System.exit(0);
					}
				} while (isContinue);
				
			}else {
				System.out.println("你还有"+(3-isLoop)+"次输入机会后系统将退出");
				isLoop++;
				if(isLoop==4) {
					System.out.println("3次机会已用完,系统退出");
					System.exit(0);
				}
			}
		} while (isLoop<4);
	}
	//检查学生是否在表中
	public boolean login(int studentNo,String pwd) throws Exception {
		try {
			getConn();
			st = conn.createStatement();
			String sql="select * from student where studentNo="+studentNo+" and loginPwd='"
					+pwd+"'";
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closeConn();
		}
		return false;	
	}
	//展示数据
	public void showData() {
		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("请输入编号:");
	}
	//1.查看所有课程信息
	public void subjectInfo() throws Exception {
		getConn();
		st = conn.createStatement();
		String sql = "select * from subject";
		ResultSet rs = st.executeQuery(sql);
		System.out.println("课程编号\t课程名称\t学分\t所属班级");
		while(rs.next()) {
			System.out.println(rs.getInt("SubjectNo")+"\t"+rs.getString("SubjectName")
					+"\t"+rs.getInt("ClassHour")+"\t"+rs.getInt("GradeId"));
		}
		closeConn();
	}
	//2.查看自己选择的课程信息
	public void selectCourse(int studentNo) throws Exception {
		getConn();
		st = conn.createStatement();
		String sql = "select * from subject where subjectNo in ((select subjectNo from result where studentNo="+studentNo+"))";
		ResultSet rs = st.executeQuery(sql);
		System.out.println("课程编号\t课程名称\t学分\t所属班级");
		while(rs.next()) {
			System.out.println(rs.getInt("SubjectNo")+"\t"+rs.getString("SubjectName")
					+"\t"+rs.getInt("ClassHour")+"\t"+rs.getInt("GradeId"));
		}
		closeConn();
	}
	//3.新增选课
	public void addCourse(int studentNo) throws Exception {
		System.out.println("请输入要选择的课程编号:");
		int subjectNo = input.nextInt();
		//判断课程是否在课程表上
		if(!isSubject(subjectNo)) {
			System.out.println("你要选的课程不存在");
		}else {
			//判断该课程是否属于该学生所在班级的可以选择的课程
			if(isGrade(subjectNo, studentNo)) {
				//判断该课程是否已经被该学生选过
				if(isExist(subjectNo, studentNo)) {
					System.out.println("你已经选择过这门课程");
				}
				else {
					//新增课程成功,存入result表
					addSubject(subjectNo, studentNo);
					System.out.println("新增科目成功");
				}
			}else{
				System.out.println("该班级的学生不可以选择该课程");
			}
		}
	}
	//3.判断课程是否在课程表上
	public boolean isSubject(int subjectNo) throws Exception {
		try {
			getConn();
			st=conn.createStatement();
			String sql = "select SubjectName from subject where SubjectNo="+subjectNo;
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closeConn();
		}
		return false;
	}
	//3.判断该课程是否属于该学生所在班级可以选择的课程
	public boolean isGrade(int SubjectNo,int studentNo) throws Exception {
		try {
			getConn();
			st = conn.createStatement();
			String sql = "select * from student where GradeId="
					+ "(select GradeId from subject where SubjectNo="+SubjectNo+") and studentNo="+studentNo;
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closeConn();
		}
		return false;
		
	}
	//3.判断该课程是否已经被该学生选过
	public boolean isExist(int subjectNo,int studentNo) throws Exception {
		try {
			getConn();
			st = conn.createStatement();
			String sql = "select studentNo from result where studentNo="+
						studentNo+" and subjectNo="+subjectNo;
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			closeConn();
		}
		return false;
		
	}
	//3.新增成功,存入result表
	public void addSubject(int subjectNo,int studentNo) throws Exception {
		getConn();
		st=conn.createStatement();
		//获得系统时间
		Date date = new Date();
		//将时间格式转换成符合Timestamp要求的格式
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        String sql = "insert into result(studentNo,subjectNo,examDate) values("+studentNo+","+subjectNo+",'"+nowTime+"')";
		st.executeUpdate(sql);
		closeConn();
	}
	//4.查看自己每门课程的成绩
	public void lookResult(int studentNo) throws Exception {
		getConn();
		st = conn.createStatement();
		String sql = "select s.subjectNo,SubjectName,studentResult from \r\n" + 
				"(select * from result where StudentNo="+studentNo+") s left join subject\r\n" + 
				"on s.subjectNo=subject.SubjectNo";
		ResultSet rs = st.executeQuery(sql);
		System.out.println("课程编号\t课程名称\t成绩");
		while(rs.next()) {
			System.out.println(rs.getInt("s.SubjectNo")+"\t"+rs.getString("SubjectName")
					+"\t"+rs.getInt("studentResult"));
		}
		closeConn();
	}
	//5.查看自己班级信息
	public void gradeInfo(int studentNo) throws Exception {
		getConn();
		st = conn.createStatement();
		String sql = "select s.gradeid,gradename from (select gradeid from student where studentno="+studentNo+") s \r\n" + 
				"left join grade on s.gradeid=grade.GradeId";
		ResultSet rs = st.executeQuery(sql);
		System.out.println("班级编号\t班级名称");
		while(rs.next()) {
			System.out.println(rs.getInt("gradeid")+"\t"+rs.getString("gradename"));
		}
		closeConn();
	}
	//6.查看自己班级的同学
	public void classmates(int studentNo) throws Exception {
		getConn();
		st = conn.createStatement();
		String sql = "select studentno,studentname,sex,gradeid from student WHERE\r\n" + 
				"gradeid=(select gradeid from student where studentno="+studentNo+") ";
		ResultSet rs = st.executeQuery(sql);
		System.out.println("学号\t名字\t性别\t班级编号");
		while(rs.next()) {
			System.out.println(rs.getInt("studentno")+"\t"+rs.getString("studentname")+"\t"
						+rs.getInt("sex")+"\t"+rs.getInt("gradeid"));
		}
		closeConn();
	}
	//7.作废某门课程
	public void deleteCourse(int studentNo) throws Exception {
		System.out.println("请输入要作废的课程的编号:");
		int subjectNo = input.nextInt();
		//判断该门课程是否已经被该学生选过
		if(isExist(subjectNo, studentNo)) {
			getConn();
			st = conn.createStatement();
			String sql = "delete from result where studentno="+studentNo+" and subjectno="+subjectNo;
			int rs = st.executeUpdate(sql);	
			if(rs>0) {
				System.out.println("该门课程作废成功");
			}
			closeConn();
		}else {
			System.out.println("该学生没有选择过该门课程");
		}
	}
	//8.修改密码
	public void updatePwd(int studentNo) throws Exception {
		System.out.println("请输入修改后的密码");
		String pwd = input.next();
		getConn();
		st = conn.createStatement();
		String sql = "update student set loginPwd='"+pwd+"' where studentno="+studentNo;
		int rs=st.executeUpdate(sql);
		if(rs>0) {
			System.out.println("修改成功");	
		}
		closeConn();
	}
}

Test.java
测试

package studentSelect;

public class Test {
	public static void main(String[] args) throws Exception {
		Util util = new Util();
		util.courseSystem();
	}
}

效果图:
简单的学生选课模拟系统(java)_第10张图片

你可能感兴趣的:(数据库连接技术,mysql,数据库,java,jdbc,sql)