Last active
August 29, 2015 13:55
-
-
Save NusZzz/8736025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.Scanner; | |
import com.mysql.jdbc.Statement; | |
public class Main { | |
/** | |
* @param args | |
* @throws ClassNotFoundException | |
* @throws SQLException | |
*/ | |
public static void main(String[] args) throws Exception { | |
Scanner keyboardInput = new Scanner(System.in); | |
System.out.println("=========== MENU ==========="); | |
System.out.println("1.) Register"); | |
System.out.println("2.) Login"); | |
System.out.print("Enter Choice: "); | |
try{ | |
int choiceKeyboard = keyboardInput.nextInt(); | |
if (choiceKeyboard==1){ | |
insertMember(); | |
main(args); | |
}else if (choiceKeyboard==2){ | |
login(); | |
}else { | |
System.out.println("Please select 1 or 2."); | |
main(args); | |
} | |
}catch(Exception e){ | |
System.out.println("Please select 1 or 2."); | |
main(args); | |
} | |
} | |
public static void login() throws ClassNotFoundException, SQLException { | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataBase","root",""); | |
System.out.println("\n=========== Input information ==========="); | |
Scanner keyboardInput = new Scanner(System.in); | |
System.out.print(" Enter Login Name: "); | |
String nameInput = keyboardInput.nextLine(); | |
PreparedStatement statement = con.prepareStatement("SELECT * from user WHERE USER_FNAME = '" + nameInput + "'"); | |
ResultSet result = statement.executeQuery(); | |
String USER_ID = null; | |
if(result.next()){ | |
USER_ID = result.getString(3); | |
System.out.print(" Enter Login Password: "); | |
String surnameInput = keyboardInput.nextLine(); | |
if(surnameInput.equals(result.getString(3))){ | |
System.out.println("\nWelcome to enrollment system.\n"); | |
USER_ID = result.getString(1); | |
int USER_ID_INT = Integer.parseInt(USER_ID); | |
menuUser(USER_ID_INT); | |
}else{ | |
System.out.println("Sorry, your password is wrong! Please login again."); | |
login(); | |
} | |
}else{ | |
System.out.println("Sorry, system has no this account! please login again"); | |
login(); | |
} | |
} | |
public static void examTable(int userID) throws SQLException{ | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataBase","root",""); | |
PreparedStatement statement = con.prepareStatement("SELECT * from course WHERE USER_ID = " + userID + ""); | |
ResultSet result = statement.executeQuery(); | |
System.out.println("\n========== Examination Table =========="); | |
System.out.println("Course ID " + "Course Name " + "Course Time"); | |
while (result.next()){ | |
System.out.println(result.getString(1) + " " +result.getString(3)+ "\t\t" +result.getString(2)); | |
} | |
menuUser(userID); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void menuUser(int USER_ID_INT){ | |
System.out.println("\n=========== MENU ==========="); | |
System.out.println("Select 1 for course enrollment"); | |
System.out.println("Select 2 for knowing exam table"); | |
System.out.println("Select 3 for exit"); | |
System.out.print("Enter your selection: "); | |
try{ | |
Scanner keyboardInput = new Scanner(System.in); | |
int choiceInput = keyboardInput.nextInt(); | |
if (choiceInput==1){ | |
insertCourse(USER_ID_INT); | |
}else if (choiceInput==2){ | |
examTable(USER_ID_INT); | |
}else if (choiceInput==3){ | |
System.out.print("Good bye!"); | |
}else{ | |
System.out.println("Please select 1 or 2. Please login again"); | |
menuUser(USER_ID_INT); | |
} | |
}catch(Exception e){ | |
System.out.println("Please select 1 or 2. Please choose again"); | |
menuUser(USER_ID_INT); | |
} | |
} | |
public static void insertCourse(int userID) throws ClassNotFoundException, SQLException{ | |
Class.forName("com.mysql.jdbc.Driver"); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataBase","root",""); | |
PreparedStatement statement = con.prepareStatement("SELECT * from courselist"); | |
ResultSet result = statement.executeQuery(); | |
int counter = 0; | |
while (result.next()){ | |
counter++; | |
System.out.println(counter + ".) Course ID: " + result.getString(1) + " \tCourse Name: " +result.getString(2)+ " \tCourse Time: " +result.getString(3)+ " \tCourse Examination Date: " +result.getString(4)); | |
} | |
Scanner inputCourse = new Scanner(System.in); | |
System.out.print("\nEnter course you want to enroll(input 0 to exit to menu): "); | |
try{ | |
int input = inputCourse.nextInt(); | |
PreparedStatement statementCourse = null; | |
if(input==1){ | |
PreparedStatement checkCourseStatement = con.prepareStatement("SELECT * from course WHERE C_ID='1302001' AND USER_ID= '" + userID + "'"); | |
ResultSet resultCheckCourseStatement = checkCourseStatement.executeQuery(); | |
int count = 0; | |
while(resultCheckCourseStatement.next()){ | |
count++; | |
} | |
if(count==0){ | |
statementCourse = con.prepareStatement("SELECT * from courselist WHERE C_ID= '" + 1302001 + "'"); | |
ResultSet resultCourse = statementCourse.executeQuery(); | |
String C_ID = null, C_Sub = null, C_Time = null, C_Exam = null; | |
while (resultCourse.next()){ | |
C_ID = resultCourse.getString(1); | |
C_Sub = resultCourse.getString(2); | |
C_Time = resultCourse.getString(3); | |
C_Exam = resultCourse.getString(4); | |
} | |
String query = " insert into course (C_ID, C_Time, C_Sub, USER_ID)" + " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt = con.prepareStatement(query); | |
preparedStmt.setString (1, C_ID); | |
preparedStmt.setString (2, C_Time); | |
preparedStmt.setString (3, C_Sub); | |
preparedStmt.setInt (4, userID); | |
preparedStmt.execute(); | |
String query1 = " insert into examination (USER_ID, E_ID, E_Sub, E_Time)" + " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt1 = con.prepareStatement(query1); | |
preparedStmt1.setInt (1, userID); | |
preparedStmt1.setString (2, C_ID); | |
preparedStmt1.setString (3, C_Sub); | |
preparedStmt1.setString (4, C_Exam); | |
preparedStmt1.execute(); | |
System.out.print("Enrollment is success!\n"); | |
menuUser(userID); | |
}else{ | |
System.out.println("\nYou used to register this course.\n"); | |
insertCourse(userID); | |
} | |
}else if (input==2){ | |
PreparedStatement checkCourseStatement = con.prepareStatement("SELECT * from course WHERE C_ID='1302002' AND USER_ID= '" + userID + "'"); | |
ResultSet resultCheckCourseStatement = checkCourseStatement.executeQuery(); | |
int count = 0; | |
while(resultCheckCourseStatement.next()){ | |
count++; | |
} | |
if(count==0){ | |
statementCourse = con.prepareStatement("SELECT * from courselist WHERE C_ID= '" + 1302002 + "'"); | |
ResultSet resultCourse = statementCourse.executeQuery(); | |
String C_ID = null, C_Sub = null, C_Time = null, C_Exam = null; | |
while (resultCourse.next()){ | |
C_ID = resultCourse.getString(1); | |
C_Sub = resultCourse.getString(2); | |
C_Time = resultCourse.getString(3); | |
C_Exam = resultCourse.getString(4); | |
} | |
String query = " insert into course (C_ID, C_Time, C_Sub, USER_ID)" + " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt = con.prepareStatement(query); | |
preparedStmt.setString (1, C_ID); | |
preparedStmt.setString (2, C_Time); | |
preparedStmt.setString (3, C_Sub); | |
preparedStmt.setInt (4, userID); | |
preparedStmt.execute(); | |
String query1 = " insert into examination (USER_ID, E_ID, E_Sub, E_Time)" + " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt1 = con.prepareStatement(query1); | |
preparedStmt1.setInt (1, userID); | |
preparedStmt1.setString (2, C_ID); | |
preparedStmt1.setString (3, C_Sub); | |
preparedStmt1.setString (4, C_Exam); | |
preparedStmt1.execute(); | |
System.out.print("Enrollment is success!\n"); | |
menuUser(userID); | |
}else{ | |
System.out.println("\nYou used to register this course.\n"); | |
insertCourse(userID); | |
} | |
}else if (input==3){ | |
PreparedStatement checkCourseStatement = con.prepareStatement("SELECT * from course WHERE C_ID='1302003' AND USER_ID= '" + userID + "'"); | |
ResultSet resultCheckCourseStatement = checkCourseStatement.executeQuery(); | |
int count = 0; | |
while(resultCheckCourseStatement.next()){ | |
count++; | |
} | |
if(count==0){ | |
statementCourse = con.prepareStatement("SELECT * from courselist WHERE C_ID= '" + 1302003 + "'"); | |
ResultSet resultCourse = statementCourse.executeQuery(); | |
String C_ID = null, C_Sub = null, C_Time = null, C_Exam = null; | |
while (resultCourse.next()){ | |
C_ID = resultCourse.getString(1); | |
C_Sub = resultCourse.getString(2); | |
C_Time = resultCourse.getString(3); | |
C_Exam = resultCourse.getString(4); | |
} | |
String query = " insert into course (C_ID, C_Time, C_Sub, USER_ID)"+ " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt = con.prepareStatement(query); | |
preparedStmt.setString (1, C_ID); | |
preparedStmt.setString (2, C_Time); | |
preparedStmt.setString (3, C_Sub); | |
preparedStmt.setInt (4, userID); | |
preparedStmt.execute(); | |
String query1 = " insert into examination (USER_ID, E_ID, E_Sub, E_Time)" + " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt1 = con.prepareStatement(query1); | |
preparedStmt1.setInt (1, userID); | |
preparedStmt1.setString (2, C_ID); | |
preparedStmt1.setString (3, C_Sub); | |
preparedStmt1.setString (4, C_Exam); | |
preparedStmt1.execute(); | |
System.out.print("Enrollment is success!\n"); | |
menuUser(userID); | |
}else{ | |
System.out.println("\nYou used to register this course.\n"); | |
insertCourse(userID); | |
} | |
}else if (input==4){ | |
PreparedStatement checkCourseStatement = con.prepareStatement("SELECT * from course WHERE C_ID='1302004' AND USER_ID= '" + userID + "'"); | |
ResultSet resultCheckCourseStatement = checkCourseStatement.executeQuery(); | |
int count = 0; | |
while(resultCheckCourseStatement.next()){ | |
count++; | |
} | |
if(count==0){ | |
statementCourse = con.prepareStatement("SELECT * from courselist WHERE C_ID= '" + 1302004 + "'"); | |
ResultSet resultCourse = statementCourse.executeQuery(); | |
String C_ID = null, C_Sub = null, C_Time = null, C_Exam = null; | |
while (resultCourse.next()){ | |
C_ID = resultCourse.getString(1); | |
C_Sub = resultCourse.getString(2); | |
C_Time = resultCourse.getString(3); | |
C_Exam = resultCourse.getString(4); | |
} | |
String query = " insert into course (C_ID, C_Time, C_Sub, USER_ID)"+ " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt = con.prepareStatement(query); | |
preparedStmt.setString (1, C_ID); | |
preparedStmt.setString (2, C_Time); | |
preparedStmt.setString (3, C_Sub); | |
preparedStmt.setInt (4, userID); | |
preparedStmt.execute(); | |
String query1 = " insert into examination (USER_ID, E_ID, E_Sub, E_Time)" | |
+ " values (?, ?, ?, ?)"; | |
PreparedStatement preparedStmt1 = con.prepareStatement(query1); | |
preparedStmt1.setInt (1, userID); | |
preparedStmt1.setString (2, C_ID); | |
preparedStmt1.setString (3, C_Sub); | |
preparedStmt1.setString (4, C_Exam); | |
preparedStmt1.execute(); | |
System.out.print("Enrollment is success!\n"); | |
menuUser(userID); | |
}else{ | |
System.out.println("\nYou used to register this course.\n"); | |
insertCourse(userID); | |
} | |
}else if(input==0){ | |
menuUser(userID); | |
}else{ | |
System.out.println("\nPlease enter number 1-4!"); | |
insertCourse(userID); | |
} | |
}catch(Exception e){ | |
System.out.println("\nPlease enter number only!"); | |
insertCourse(userID); | |
} | |
} | |
public static void insertMember() throws ClassNotFoundException, SQLException{ | |
System.out.println("\nPlease fill your information"); | |
Scanner input = new Scanner(System.in); | |
System.out.print("Enter Name: "); | |
String name = input.nextLine(); | |
System.out.print("Enter Surname: "); | |
String surName = input.nextLine(); | |
System.out.print("Enter Nickname: "); | |
String nickName = input.nextLine(); | |
System.out.print("Enter Gender (Male/Female): "); | |
String gender = input.nextLine(); | |
System.out.print("Enter Address: "); | |
String address = input.nextLine(); | |
System.out.print("Enter Phone number: "); | |
String phoneNumber = input.nextLine(); | |
System.out.print("Enter School: "); | |
String school = input.nextLine(); | |
System.out.print("Enter Expected University: "); | |
String expected = input.nextLine(); | |
System.out.print("Enter your father name: "); | |
String fatherName = input.nextLine(); | |
System.out.print("Enter birth date (DD/MM/YYYY): "); | |
String birthDate = input.nextLine(); | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url ="jdbc:mysql://localhost:3306/Database"; | |
Connection con = DriverManager.getConnection(url,"root",""); | |
String insertMySQL = "INSERT INTO user (USER_FNAME, USER_LNAME, School, ExpectedUni, USER_Phone, USER_Address, USER_Family, USER_Nickname, USER_Sex) " | |
+ "VALUES ('" + name +"', '" + surName +"', '" + school +"', '" + expected +"', '" + phoneNumber +"', '" + address +"', '" + fatherName +"', '" + nickName +"', '" + gender +"')"; | |
try { | |
Statement statement = (Statement) con.createStatement(); | |
statement.execute(insertMySQL); | |
System.out.println("Registr Success!"); | |
} catch (SQLException ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} catch (Exception ex) { | |
System.out.println(ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment