JAVA | Student Course Selection System

Source: Internet
Author: User
Tags id3

The simple student selection system, written in the Java language, shows the core code of the system.

Some of them are not enough to welcome criticism and correction!

Code for linked databases

Package connection;
Connecting to a database student

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.Statement;
Import java.sql.SQLException;

public class xu{

private static Connection conn = null;
static {
try {
Registration driver
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
Get a database connection
conn = Drivermanager.getconnection ("jdbc:sqlserver://localhost:1433;databasename=student", "sa", "123456");
}catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public static Connection getconnection () {
Return conn;
}
}

Operation Code of Elective course

Package connection;

Import java.sql.*;
Import java.util.*;

public class test1{

private static String id;//here the ID is set to a global variable, in some of the following methods will be used
private static Scanner console = new Scanner (system.in);
private static String Managerpass = "123456";
private static Connection conn = Xu.getconnection ();


public static void Main (String [] args) throws SQLException {

System.out.println ("Welcome to the student selection system!\n\n"
+ "If you is Maneger please choose 6, 7, 8, 9, 10\n"
+ "If you is student please choose 1, 2, 3, 4, 5, 10\n");
ShowMenu ();
}

public static void ShowMenu () throws SQLException {

System.out.println ("-----------option-------------\ n");

SYSTEM.OUT.PRINTLN ("1--student login"); Student Login
System.out.println ("2--change password"); Student Change Password
System.out.println ("3--choose course"); Student Selection Course
System.out.println ("4--get course list"); Students to view their curriculum
System.out.println ("5--student rigistration"); Student Registration Account
System.out.println ("6--create student"); Administrators create students
System.out.println ("7--create course"); Administrator creates a timetable
System.out.println ("8--get student List"); Administrator View Student Table
System.out.println ("9--get course List1"); Administrators view courses in the selected course
System.out.println ("10--exit\n"); Exit

Test ();
}



public static void Test () throws SQLException {

SYSTEM.OUT.PRINTLN ("Manager choose 1, student choose 0");
int choose1 = Console.nextint (); int with Nextint ()

if (choose1 = = 1) {

SYSTEM.OUT.PRINTLN ("Please input your password:");
String password = console.next (); String with Next ()

if (Password.equals (Managerpass)) {

System.out.println ("Welcome manager!\n");
ManagerMenu1 ();
}

else {

System.out.println ("Your password is worry!"); }
}

else if (choose1 = = 0) {

System.out.println ("Welcome student!");
StudentMenu1 ();
}
}



/* Administrator Action menu */
public static void ManagerMenu1 () throws SQLException {

System.out.println ("----------Manager----------\ n");
System.out.println ("choose option");
int choose2 = Console.nextint ();

Switch (CHOOSE2) {

Case 1:
Case 2:
Case 3:
Case 4:
Case 5:
System.out.println ("This is student ' s option,try again!");
ManagerMenu1 ();
Break
Case 6:createstu ();
Case 7:createcourse ();
Case 8:getstudentlist ();
Case 9:getcourselist1 ();
Case 10:system.exit (0);
DEFAULT:SYSTEM.OUT.PRINTLN ("Please input a right number, try again!"); ManagerMenu1 ();

}
}



/* Student Action menu */
public static void StudentMenu1 () throws SQLException {

System.out.println ("----------Student----------\ n");
SYSTEM.OUT.PRINTLN ("Please input option");
int choose3 = Console.nextint ();

Switch (CHOOSE3) {

Case 1:stulogin ();
Case 2:changepass ();

Students are required to perform stulogin operations before choose Course,get Course list

Case 3:
Case 4:SYSTEM.OUT.PRINTLN ("You must login before your can do it!\n"); Stulogin (); break;
Case 5:sturigistration ();
Case 6:
Case 7:
Case 8:
Case 9:system.out.println ("This is manager ' s option,try again!"); StudentMenu1 (); break;
Case 10:system.exit (0);
DEFAULT:SYSTEM.OUT.PRINTLN ("Please input a right number,try again!"); StudentMenu1 ();

}
}



/* Administrator Create course */
public static void Createcourse () throws sqlexception{

try {
/* Creates a statement object database operand by connection the database link object, and the Stat object obtains a statement that executes SQL.
* Stat object is a Java program and database of a connection channel * *

Statement stat = conn.createstatement ();
String SQL1 = "INSERT into course (Couid,couname,couteacher) VALUES (1, ' Software engineering ', ' Huang Weiguo delighted ')";
String sql2 = "INSERT into course (Couid,couname,couteacher) VALUES (2, ' database ', ' Vermian ')";
String sql3 = "INSERT into course (Couid,couname,couteacher) VALUES (3, ' algorithmic design ', ' Dawn ')";
String Sql4 = "INSERT into course (Couid,couname,couteacher) VALUES (4, ' College English ', ' Zhangchen ')";
String sql5 = "INSERT into course (Couid,couname,couteacher) VALUES (5, ' Java ', ' Tangyahuan ')";

int a = Stat.executeupdate (SQL1);
int b = Stat.executeupdate (SQL2);
int c = stat.executeupdate (SQL3);
int d = stat.executeupdate (SQL4);
int e = stat.executeupdate (SQL5);

int sum = a + B + C + D + E;
System.out.println ("Add successfully" + Sum + "line course data");
ManagerMenu1 ();

} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
System.out.println ("Course already exists");

ManagerMenu1 ();
}
}



/* Administrator Create student */
public static void Createstu () throws SQLException {


try {
Statement stat = conn.createstatement ();
String SQL1 = "INSERT into student (Stuid,stuname) Values (20160501, ' Xu Ming Positive ')";

The input of student information here must also be an input, which is the system defect, the problem to be solved

int a = Stat.executeupdate (SQL1);
SYSTEM.OUT.PRINTLN ("successfully added" + A + "row student data");
ManagerMenu1 ();

}catch (SQLException e) {
TODO auto-generated Catch block
System.out.println ("The student already exists");

ManagerMenu1 ();
}
}


/* Student Login */
public static void Stulogin () throws SQLException {

SYSTEM.OUT.PRINTLN ("Please input your stuid"); Enter your account
ID = Console.next ();

SYSTEM.OUT.PRINTLN ("Please input your Password"); Enter password
String Stupassword = Console.next ();

String stuid = null;
String Password = null;

Statement Stat;
ResultSet rs;

try {
Stat = Conn.createstatement ();
String SQL1 = "SELECT * from Login"; /* All of its property values are queried from the login table, both Stuid and password.
RS is the result set. The queried record is a list, with the initial pointer pointing to the first record. */
Boolean flag;
rs = Stat.executequery (SQL1);

while (flag = Rs.next ()) {//Rs.next ();//return value is TRUE or False

Stuid = rs.getstring ("Stuid");
Password = rs.getstring ("Password");

if (Id.equals (stuid) && stupassword.equals (Password))//note here, the condition of jumping out of the loop is important

Break

False if Stuid and password,flag that match user input stuid and password are not found in the database

}

if (!flag) {//when flag is false

System.out.println ("The ID or Password is worry! Please input your ID and Password again\n ");
Stulogin ();
}

else {
SYSTEM.OUT.PRINTLN ("* * *" +id+ "* * *" +STUID + "* * *" +stupassword+ "* * *" +password+ "* *");

System.out.println ("Login successful!\n");
System.out.println ("------------------choose option------------------\ n"
+ "If you want to change Password, please choose 2\n"
+ "If you want to Choose course,please Choose 3\n" + "If you want to Get Course list,please Choose 4\n"
+ "If you want to exit, please choose 8\n");

int choose4 = Console.nextint ();

Switch (CHOOSE4) {

Case 2:changepass ();
Case 3:choosecourse ();
Case 4:getcourselist ();
Case 10:system.exit (0);
DEFAULT:SYSTEM.OUT.PRINTLN ("You input an invalid number!");

}

}

} catch (SQLException e) {

TODO auto-generated Catch block
E.printstacktrace ();
}

}

/* Here is a description of the difference between the methods execute, ExecuteQuery, executeupdate
The * method executequery a statement for a single result set, such as SELECT. He will put the results of the query into the ResultSet class object for use.
* Method Executeupdate is used to execute INSERT, UPDATE, or DELETE statements < return values are integers > and SQL DDL (data definition language), such as CREATE table and drop table < return value is zero >.
* Method Execute is used to perform statements that return multiple result sets, multiple update counts, or a combination of both. */



/* Student Change Password */
public static void Changepass () throws SQLException {

System.out.println (ID);

/* The ID here should have to be re-entered, otherwise there is no corresponding id*/when you choose to modify the password of an ID update in the database

SYSTEM.OUT.PRINTLN ("Please input your ID");
String ID = Console.next ();
SYSTEM.OUT.PRINTLN ("Please input your new Password");
String newpassword = Console.next ();

Statement Stat;

try {

Stat = Conn.createstatement (); Both of these can be
Stat = conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
String sql = "UPDATE login SET Password =" + "" + NewPassword + "where Stuid =" + ID;

Note that the NewPassword and ID here are not "enclosed"

String SQL1 = "UPDATE login SET Password = 111 where stuid = 20156022";

int a = stat.executeupdate (SQL);
System.out.println (a);

if (A! = 0) {

SYSTEM.OUT.PRINTLN ("Successfully modified" +a+ "line Record");
System.out.println ("Your new Password is" + newpassword);

StudentMenu1 ();
}

ElseSystem.out.println ("\ n" + "changing the Password is failed!");

}catch (SQLException e) {

TODO auto-generated Catch block
E.printstacktrace ();
}

}



/* Students Choose class * *
public static void Choosecourse () throws SQLException {

System.out.println (ID);
String couid = null;
String couname = null;
String couteacher = null;

Statement Stat;
ResultSet rs;

try {

Stat = Conn.createstatement ();
String sql = "SELECT * from Course";
rs = stat.executequery (SQL);

SYSTEM.OUT.PRINTLN ("--------information-------of optional courses");
while (Rs.next ()) {

Couid = rs.getstring ("Couid");
Couname = rs.getstring ("Couname");
Couteacher = rs.getstring ("Couteacher");

System.out.println (couid + "+ couname +" "+ couteacher);
}

SYSTEM.OUT.PRINTLN ("\ Choose your course \ n");

stat= conn.createstatement ();

This stuid the ID used by a fixed student to log in, which ensures that you can sign in to your account and choose another account.

SYSTEM.OUT.PRINTLN ("Input CourseID");
int ID1 = Console.nextint ();
int ID2 = Console.nextint ();
int ID3 = Console.nextint ();

This way, the number of students should be fixed to 3, the problem to be solved

String SQL1 = "INSERT into Stucourse (STUID,COUID1,COUID2,COUID3) VALUES (" + ID + "," + ID1 + "," + ID2 + "," +ID3 + ")";
int i = stat.executeupdate (SQL1);

if (i! = 0) {
SYSTEM.OUT.PRINTLN ("Success in Course selection");

Getcourselist ();
}
else System.out.println ("Optional Course error");

} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}



/* Students get a list of selected courses */
public static void Getcourselist () throws SQLException {

System.out.println ("\nhere is your course so you have choose\n");

Statement stat = null;
ResultSet rs = null;

try {

Stat = Conn.createstatement ();
String sql = "SELECT * from Stucourse";
rs = stat.executequery (SQL);

String stuid = null;
String couID1 = null;
String couID2 = null;
String couID3 = null;
String couID4 = null;

while (Rs.next ()) {

Stuid = rs.getstring ("Stuid");
couID1 = rs.getstring ("couID1");
couID2 = rs.getstring ("couID2");
CouID3 = rs.getstring ("couID3");
CouID4 = rs.getstring ("couID4");


if (Stuid.equals (ID))

System.out.println (stuid + "+ couID1 +" "+ couID2 +" "+ couID3 +" + couID4 + "\ n");

}

}catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}



/* Student Registration */
public static void Sturigistration () throws sqlexception{

SYSTEM.OUT.PRINTLN ("Please input your ID");
String ID = Console.next ();
SYSTEM.OUT.PRINTLN ("Please input your Password");
String Password = Console.next ();

try {
Statement stat = conn.createstatement ();
String SQL1 = "INSERT INTO Login" (Stuid,password) Values ("+ ID +", "+ Password +") ";
Here the password can only enter the number, the problem to be resolved

int a = Stat. executeupdate (SQL1);
SYSTEM.OUT.PRINTLN ("Successful registration" + A + "account");

StudentMenu1 ();

}catch (SQLException e) {
TODO auto-generated Catch block
System.out.println ("ID already exists");
}
}


/* Administrator View student table */
public static void Getstudentlist () {

System.out.println ("\ n here is student ' information \ n");

Statement stat = null;
ResultSet rs = null;

try {

Stat = Conn.createstatement ();
String sql = "SELECT * from student";
rs = stat.executequery (SQL);

String stuid = null;
String stuname = null;

while (Rs.next ()) {

Stuid = rs.getstring ("Stuid");
Stuname= rs.getstring ("Stuname");

System.out.println (Stuid + "" + stuname + "\ n");

ManagerMenu1 ();
}

}catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}


/* Administrator View course in selected Course */
public static void GetCourseList1 () {

System.out.println ("\ n Here is course ' information \ n");

Statement stat = null;
ResultSet rs = null;

try {

Stat = Conn.createstatement ();
String sql = "SELECT * from Course";
rs = stat.executequery (SQL);

String couid = null;
String couname = null;
String couteacher = null;


while (Rs.next ()) {

Couid = rs.getstring ("Couid");
Couname = rs.getstring ("Couname");
Couteacher = rs.getstring ("Couteacher");

System.out.println (couid + "+ couname +" "+ Couteacher +" \ n ");

ManagerMenu1 ();
}

}catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

JAVA | Student Course Selection System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.