Last active
August 29, 2015 13:55
-
-
Save NusZzz/8788386 to your computer and use it in GitHub Desktop.
Code for Network Programming Project 2
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.io.*; | |
import java.net.*; | |
import java.sql.*; | |
import java.util.*; | |
import com.mysql.jdbc.Connection; | |
import com.mysql.jdbc.Statement; | |
class Client { | |
private static int port = 12345; | |
private static String host = "localhost"; | |
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException { | |
Socket server = null; | |
PrintWriter out = null; | |
try { | |
server = new Socket(host, port); | |
out = new PrintWriter(server.getOutputStream(), true); | |
} catch (UnknownHostException e) { | |
System.err.println(e); | |
System.exit(1); | |
} | |
boolean checkExit = false; | |
do{ | |
checkExit = false; | |
System.out.println("\n================== Menu =================="); | |
System.out.println("1.) Show all sudents's score"); | |
System.out.println("2.) Add sudents's score"); | |
System.out.println("3.) Delete sudents's score"); | |
System.out.println("4.) Search sudents's score"); | |
System.out.println("5.) Exit"); | |
System.out.print("Enter your choice: "); | |
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); | |
String msg = stdIn.readLine(); | |
if(msg.equals("1")){ | |
showAll(); | |
}else if(msg.equals("2")){ | |
addScore(); | |
}else if(msg.equals("3")){ | |
deleteScore(); | |
}else if(msg.equals("4")){ | |
searchStudent(); | |
}else if(msg.equals("5")){ | |
server.close(); | |
System.exit(0); | |
checkExit=true; | |
} | |
}while(checkExit=false); | |
} | |
public static void showAll() throws IOException, SQLException, ClassNotFoundException{ | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url ="jdbc:mysql://localhost:3306/testdb"; | |
Connection con = (Connection) DriverManager.getConnection(url,"root",""); | |
Statement st = (Statement) con.createStatement(); | |
ResultSet rs = st.executeQuery("SELECT * FROM student ORDER BY studentID ASC"); | |
System.out.println("\n============= Show all students score =============\n"); | |
int count = 0; | |
while (rs.next()) { | |
count++; | |
System.out.println(count + ".) " + "Student ID = " +rs.getString(1)+ ", Score = "+rs.getString(2)); | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} finally{ | |
main(null); | |
} | |
} | |
public static void addScore() throws IOException, SQLException, ClassNotFoundException{ | |
Scanner kb = new Scanner(System.in); | |
System.out.print("Enter ID to add: "); | |
String id = kb.nextLine(); | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url ="jdbc:mysql://localhost:3306/testdb"; | |
Connection con = (Connection) DriverManager.getConnection(url,"root",""); | |
Statement st = (Statement) con.createStatement(); | |
ResultSet rs = st.executeQuery("SELECT * FROM student WHERE studentID = '" + id + "'"); | |
int count = 0; | |
while (rs.next()) { | |
count++; | |
} | |
if(count==0){ | |
try { | |
System.out.print("Enter Score to add: "); | |
int score = kb.nextInt(); | |
String insertMySQL = "INSERT INTO student (studentID, studentScore) VALUES (" + id +"," + score + ")"; | |
Statement statement = (Statement) con.createStatement(); | |
statement.execute(insertMySQL); | |
System.out.println("Score is added to database"); | |
main(null); | |
} catch (SQLException e) { | |
System.out.println(e.getMessage()); | |
} | |
}else{ | |
System.out.println("This id is added already in database"); | |
} | |
System.out.println(); | |
} catch (Exception e) { | |
System.out.println(e); | |
} finally{ | |
main(null); | |
} | |
} | |
public static void deleteScore() throws IOException, SQLException, ClassNotFoundException{ | |
Scanner kb = new Scanner(System.in); | |
System.out.print("Enter ID: "); | |
String id = kb.nextLine(); | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url ="jdbc:mysql://localhost:3306/testdb"; | |
Connection con = (Connection) DriverManager.getConnection(url,"root",""); | |
Statement st = (Statement) con.createStatement(); | |
ResultSet rs = st.executeQuery("SELECT * FROM student WHERE studentID = '" + id + "'"); | |
int count = 0; | |
while (rs | |
.next()) { | |
count++; | |
} | |
if(count==0){ | |
System.out.print("The database has no thid id!"); | |
main(null); | |
}else{ | |
try { | |
String deleteTableSQL = "DELETE FROM student WHERE studentID = "+id; | |
try { | |
Statement statement = (Statement) con.createStatement(); | |
statement.execute(deleteTableSQL); | |
System.out.println("Score is deleted from database"); | |
} catch (SQLException e) { | |
System.out.println(e.getMessage()); | |
} | |
System.out.println(); | |
System.out.println("Connection database completed !!"); | |
} catch (Exception e) { | |
System.out.println(e); | |
} finally{ | |
main(null); | |
} | |
} | |
} | |
public static void searchStudent() throws IOException, SQLException, ClassNotFoundException{ | |
Scanner kb = new Scanner(System.in); | |
System.out.print("Enter ID to search: "); | |
String id = kb.nextLine(); | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
String url ="jdbc:mysql://localhost:3306/testdb"; | |
Connection con = (Connection) DriverManager.getConnection(url,"root",""); | |
Statement st = (Statement) con.createStatement(); | |
ResultSet rs = st.executeQuery("SELECT * FROM student WHERE studentID = "+ id); | |
System.out.println("\n============= Show search student score =============\n"); | |
while (rs.next()) { | |
System.out.println("\tStudent ID = " +rs.getString(1)+ ", Score :"+rs.getString(2)); | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} finally{ | |
main(null); | |
} | |
} | |
} |
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.net.ServerSocket; | |
import java.net.Socket; | |
import java.io.IOException; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
class Server { | |
private static int port = 12345; /* port the server listens on */ | |
public static void main (String[] args) throws IOException { | |
ServerSocket server = null; | |
try { | |
server = new ServerSocket(port); /* start listening on the port */ | |
System.out.println("\nServer is waiting for connecting from Client"); | |
} catch (IOException e) { | |
System.err.println("Could not listen on port: " + port); | |
System.err.println(e); | |
System.exit(1); | |
} | |
Socket client = null; | |
try { | |
client = server.accept(); | |
System.out.println("Client is connected with Server"); | |
} catch (IOException e) { | |
System.err.println("Accept failed."); | |
System.err.println(e); | |
System.exit(1); | |
} | |
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); | |
String msg= null; | |
while(true){ | |
msg = in.readLine(); | |
System.out.println("Client choose: " + msg); | |
if(msg.equals("1")){ | |
String[] splitString = msg.split(" "); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment