数据库--exp--一个简单的选课系统

//---------------------------------------------------------------------------------------------------------------

1. 数据库schema的相关关系:

S  (SID,SNAME,AGE,SEX)

中文语义:学生(学号,姓名,年龄,性别)

SC (SID,CID,GRADE)

中文语义:学习(学号,课程号,成绩)

C  (CID,CNAME,TEACHER)

中文语义:课程(课程号,课程名,任课教师)

//---------------------------------------------------------------------------------------------------------------

2.功能要求

 

a)  读取可选课程信息;

b)  选课;

c)  退选课程;

d)  查看课程选修情况

//----------------------------------------------------------------------------------------------------------------java源码

package sql;
import java.sql.*;
import java.util.*;
public class MySQL {

    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/school1?serverTimezone=UTC";
  
    // 数据库的用户名与密码
    static final String USER = "root";
    static final String PASS = "zsy";
    private static Scanner console = new Scanner(System.in);
    private static Connection conn;
    public static void main(String[] args) {
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);
        
            // a.数据库链接
            System.out.println("连接数据库");
            conn = DriverManager.getConnection(DB_URL,USER,PASS); 
            //job
            System.out.println("Welcome !\n");
            Menu();
            // 完成后关闭
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
    public static void Menu() throws SQLException {

        System.out.println("--------------------OPTIONS---------------------");
        System.out.println("1.log in");
        System.out.println("0.eixt");

        MenuForStu();
    }
    /**
     * menu
     * @throws SQLException 
     *  */
    private static void MenuForStu() throws SQLException {
        System.out.println("Please input you option number:");
        int info = console.nextInt();
        do {
            switch (info) {
            case 1:
                login(); //登录
                break;
            case 0:
                exit(); //退出
                break;
            default:
                System.out.println("Please input the right number!TRY AGAIN!");
            }
        } while (info != 0);
    }
//------------------------------------------------------------------------------------    
    public static String login() throws SQLException { //登录选课系统
        System.out.println("Please input your SID:");
        String SID = console.next();        
        Statement stmt = null;
        ResultSet rs = null;

        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT * FROM S WHERE SID= '" +SID+"';");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        if ( rs.next() ) {
            System.out.println("login success!");
            choosequest();
        } else {
            System.out.println("there is no this student!");
        }
        rs.close();
        stmt.close();
        return SID;
    }
    //-------------------------------------------选课,往SC里增加数据
    private static void choosestucourse() throws SQLException {
            // TODO Auto-generated method stub
        Statement ptmt = conn.createStatement();
        System.out.println("你现在在选课");
        System.out.println("please input SID:");
        String SID = console.next();
        System.out.println("please input CID:");
        String sql;
        String CID = console.next();
        int GRADE = 0; //这里是学生视角,无法修改grade,一律为0
        sql = "INSERT INTO SC VALUES ('"+SID+"','"+CID+"',"+GRADE+");";
        ptmt.executeUpdate(sql);
        ptmt.close();
        }
    //-------------------------------------------读取课程
    private static void getcourse() throws SQLException {
            // TODO Auto-generated method stub
        Statement stmt;
        System.out.println("所有可选的课程");
        stmt = conn.createStatement();
        String sql;
        sql = "SELECT CID, CNAME, TEACHER FROM C";
        ResultSet rs = stmt.executeQuery(sql);

        // 展开结果集数据库
        while(rs.next()){
            String CID = rs.getString("CID");
            String CNAME= rs.getString("CNAME");
            String TEACHER = rs.getString("TEACHER");

            // 输出数据
            System.out.print("CID: " + CID);
            System.out.print(", CNAME: " + CNAME);
            System.out.print(", TEACHER: " +TEACHER);
            System.out.print("\n");
        }
        stmt.close();
        rs.close();
        }

//--------------------------------------------------second menu    
    private static void choosequest() throws SQLException {
    // TODO Auto-generated method stub
        System.out.println("--------------------OPTIONS---------------------");
        System.out.println("1.list course");
        System.out.println("2.choose course");
        System.out.println("3.course info");
        System.out.println("4.quit course");
        System.out.println("5.your course");
        System.out.println("0.eixt");

        MenuForquest();
    }

    private static void MenuForquest() throws SQLException {
    // TODO Auto-generated method stub
        System.out.println("Please input you option number:");
        int info = console.nextInt();
        do {
            switch (info) {
            case 1:
                getcourse(); //读取课程
                break;
            case 2:
                choosestucourse(); //选课
                break;
            case 3:
                getstucourse(); //选课信息
                break;
            case 4:
                quitcourse(); //退课
                break;
            case 5:
                getcoursestat(); //课程情况
                break;
            case 6:
                exit(); //退出
                break;
            default:
                System.out.println("Please input the right number!TRY AGAIN!");
            }
            System.out.println("Please input you option number: ");
            info = console.nextInt();
        } while (info != 0);    
    }
    private static void getstucourse() throws SQLException {
        // TODO Auto-generated method stub
    Statement stmt;
    System.out.println("你现在在查看你的课程信息");
    stmt = conn.createStatement();
    String sql;
    System.out.println("please input SID:");
    String SID =console.next();
    sql = "SELECT CNAME, TEACHER "
            +"FROM SC NATURE JOIN C USING (CID) "
            +"where SID ='"+ SID +"';";
    ResultSet rs = stmt.executeQuery(sql);

    // 展开结果集数据库
    while(rs.next()){
        String CNAME= rs.getString("CNAME");
        String TEACHER= rs.getString("TEACHER");

        // 输出数据
        System.out.print("CNAME: " + CNAME);
        System.out.print(", TEACHER: " + TEACHER);
        System.out.print("\n");
    }
    rs.close();
    stmt.close();
    }
    private static void getcoursestat() throws SQLException {
        // TODO Auto-generated method stub
    Statement stmt;
    System.out.println("你现在在查看你的选课");
    stmt = conn.createStatement();
    String sql;
    System.out.println("please input SID:");
    String SID =console.next();
    sql = "SELECT CID, CNAME "
            +"FROM SC nature join C using (CID) "
            +"where  SID ='"+SID+"';";
    ResultSet rs = stmt.executeQuery(sql);

    // 展开结果集数据库
    while(rs.next()){
        String CID = rs.getString("CID");
        String CNAME= rs.getString("CNAME");

        // 输出数据
        System.out.print("CID: " + CID);
        System.out.print(", CNAME: " + CNAME);
        System.out.print("\n");
    }
    stmt.close();
    rs.close();
    }
    //----------------------------------------选课信息
    private static void quitcourse() throws SQLException {
        // TODO Auto-generated method stub
    Statement stmt;
    System.out.println("你现在正在退课");
    stmt = conn.createStatement();
    String sql;
    System.out.println("please input SID:");
    String SID =console.next();
    System.out.println("please input CID:");
    String CID = console.next();
    sql = "DELETE FROM SC where SID ='"+SID+"'and CID ='"+CID+"';";
    stmt.executeUpdate(sql);
    stmt.close();
    }
//----------------------------------------------------------------------------    
    public static int exit() throws SQLException { //退出选课系统
         System.out.println("-----------------byebye---------------");
         // 完成后关闭
        conn.close();
        return 2;
    }
    
    //
}

//相关多线程,异常处理并没有很好的修正,后期有时间会优化部分代码,
//console的操作并不对用户友好,后期学一下web

 

上一篇:kubernetes入门(06)kubernetes的核心概念(3)


下一篇:为Typecho增加文章阅读次数统计功能