Advertisement
Guest User

Untitled

a guest
Apr 7th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.sql.*;
  7. import java.util.ArrayList;
  8.  
  9. //Database server
  10. public class DataServer extends Thread {
  11. private String cabinNo;
  12. private Socket socket;
  13. private BufferedReader readInput;
  14. private PrintWriter writeOutput;
  15. private static ArrayList <String> usersOnline = new ArrayList<>();
  16.  
  17. public DataServer(Socket socket) {
  18. this.socket = socket;
  19. }
  20.  
  21. public void run() {
  22. boolean b = true;
  23. while (b) {
  24. try {
  25. //Declare new reader and writer to communicate with the client
  26. readInput = new BufferedReader((new InputStreamReader(socket.getInputStream())));
  27. writeOutput = new PrintWriter(socket.getOutputStream(), true);
  28. String storeInput = readInput.readLine();
  29. connectToDb(storeInput);
  30. b = false;
  31. }
  32. catch (IOException e) {
  33. System.out.println(e.getMessage());
  34. }
  35. catch (NullPointerException e){
  36. System.out.println(e.getMessage());
  37. }
  38. catch (SQLException e){
  39. System.out.println(e);
  40. }
  41. }
  42. }
  43. //Method that takes the readInput string and sorts it into a String array in String chunks
  44. public String [] processInput(String input){
  45. String [] processedInput = input.split(",");
  46. return processedInput;
  47. }
  48. //Method that checks if a user account is logged in
  49. public boolean checkUsersOnline(String input){
  50. for(int i = 0; i < usersOnline.size(); i++){
  51. if(usersOnline.get(i).equals(input)){//readInput.equals(usersOnline.get(i))
  52. return true;
  53. }
  54. else{
  55. return false;
  56. }
  57. }
  58. return false;
  59. }
  60. //Connection method that connects to a DB and performs tasks on behalf of a client
  61. public void connectToDb(String input) throws SQLException{
  62. //First line takes a String and cuts it into chunks using processInput method
  63. //The first chunk in every message is a key that is later used by a switch statement
  64. //The key is essential for the program to know which action to perform
  65. String [] storeInput = processInput(input);
  66.  
  67.  
  68. String username = "Client";
  69. String password = "ClientAccess";
  70. String databaseName = "Cruise";
  71. String databasePath = "jdbc:mysql://localhost:3306/"+databaseName+"?autoReconnect=true&useSSL=false";
  72.  
  73. //String schoolDataBase = studentnet.cst.beds.ac.uk; //school DB
  74. //localhost:3306 //local DB
  75. try {
  76. //Try to make a connection to DB
  77. java.sql.Connection connection = null;
  78. Statement statement = null;
  79. ResultSet results = null;
  80.  
  81. Class.forName("com.mysql.jdbc.Driver");
  82. connection = DriverManager.getConnection(databasePath, username, password);
  83. statement = connection.createStatement();
  84.  
  85.  
  86. //Creating a switch statement that processes the incoming message from the client
  87. //Using the value of the first element of the Array, the switch statement takes appropriate action
  88. switch (storeInput[0]) {
  89. case "1":
  90. //First switch case: Register user
  91. //statement.executeUpdate("INSERT INTO customer (cabinNo, email, userpw) VALUE ("+storeInput[1]+",'"+storeInput[2]+"','"+storeInput[3]+"');");
  92. PreparedStatement prepStatementRegister = connection.prepareStatement
  93. ("INSERT INTO customer (cabinNo, email, userpw) VALUE (?, ?, ?)");
  94. prepStatementRegister.setString(1,storeInput[1]);
  95. prepStatementRegister.setString(2,storeInput[2]);
  96. prepStatementRegister.setString(3,storeInput[3]);
  97. prepStatementRegister.executeUpdate();
  98.  
  99. /*while (results.next()) {
  100. System.out.println(results.getString("cabinNo"));
  101. }*/
  102. break;
  103. case "2":
  104. //PreparedStatement prepStatementLogIn = connection.prepareStatement("SELECT * FROM customer;");
  105. results = statement.executeQuery("SELECT * FROM customer;");
  106. while(results.next()){
  107. if(storeInput[1].equals(results.getString("cabinNo"))){
  108. if(storeInput[2].equals(results.getString("userpw")) && checkUsersOnline(storeInput[1])==false){
  109. System.out.println("logged in");
  110. usersOnline.add(storeInput[1]);
  111. cabinNo = storeInput[1];
  112. }
  113. else{
  114. System.out.println("user already online");
  115. }
  116. }
  117. }
  118. break;
  119. case "3":
  120.  
  121. System.out.println(cabinNo);
  122.  
  123. break;
  124. }
  125. }
  126. catch(SQLException e){
  127. System.out.println("SQL exception");
  128. }
  129. catch(ClassNotFoundException e){
  130. System.out.println("Class not found exception");
  131. }
  132. catch(ArrayIndexOutOfBoundsException e){
  133. System.out.println("Array Index out of bounds");
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement