Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.sql.*;
- import java.util.ArrayList;
- //Database server
- public class DataServer extends Thread {
- private String cabinNo;
- private Socket socket;
- private BufferedReader readInput;
- private PrintWriter writeOutput;
- private static ArrayList <String> usersOnline = new ArrayList<>();
- public DataServer(Socket socket) {
- this.socket = socket;
- }
- public void run() {
- boolean b = true;
- while (b) {
- try {
- //Declare new reader and writer to communicate with the client
- readInput = new BufferedReader((new InputStreamReader(socket.getInputStream())));
- writeOutput = new PrintWriter(socket.getOutputStream(), true);
- String storeInput = readInput.readLine();
- connectToDb(storeInput);
- b = false;
- }
- catch (IOException e) {
- System.out.println(e.getMessage());
- }
- catch (NullPointerException e){
- System.out.println(e.getMessage());
- }
- catch (SQLException e){
- System.out.println(e);
- }
- }
- }
- //Method that takes the readInput string and sorts it into a String array in String chunks
- public String [] processInput(String input){
- String [] processedInput = input.split(",");
- return processedInput;
- }
- //Method that checks if a user account is logged in
- public boolean checkUsersOnline(String input){
- for(int i = 0; i < usersOnline.size(); i++){
- if(usersOnline.get(i).equals(input)){//readInput.equals(usersOnline.get(i))
- return true;
- }
- else{
- return false;
- }
- }
- return false;
- }
- //Connection method that connects to a DB and performs tasks on behalf of a client
- public void connectToDb(String input) throws SQLException{
- //First line takes a String and cuts it into chunks using processInput method
- //The first chunk in every message is a key that is later used by a switch statement
- //The key is essential for the program to know which action to perform
- String [] storeInput = processInput(input);
- String username = "Client";
- String password = "ClientAccess";
- String databaseName = "Cruise";
- String databasePath = "jdbc:mysql://localhost:3306/"+databaseName+"?autoReconnect=true&useSSL=false";
- //String schoolDataBase = studentnet.cst.beds.ac.uk; //school DB
- //localhost:3306 //local DB
- try {
- //Try to make a connection to DB
- java.sql.Connection connection = null;
- Statement statement = null;
- ResultSet results = null;
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(databasePath, username, password);
- statement = connection.createStatement();
- //Creating a switch statement that processes the incoming message from the client
- //Using the value of the first element of the Array, the switch statement takes appropriate action
- switch (storeInput[0]) {
- case "1":
- //First switch case: Register user
- //statement.executeUpdate("INSERT INTO customer (cabinNo, email, userpw) VALUE ("+storeInput[1]+",'"+storeInput[2]+"','"+storeInput[3]+"');");
- PreparedStatement prepStatementRegister = connection.prepareStatement
- ("INSERT INTO customer (cabinNo, email, userpw) VALUE (?, ?, ?)");
- prepStatementRegister.setString(1,storeInput[1]);
- prepStatementRegister.setString(2,storeInput[2]);
- prepStatementRegister.setString(3,storeInput[3]);
- prepStatementRegister.executeUpdate();
- /*while (results.next()) {
- System.out.println(results.getString("cabinNo"));
- }*/
- break;
- case "2":
- //PreparedStatement prepStatementLogIn = connection.prepareStatement("SELECT * FROM customer;");
- results = statement.executeQuery("SELECT * FROM customer;");
- while(results.next()){
- if(storeInput[1].equals(results.getString("cabinNo"))){
- if(storeInput[2].equals(results.getString("userpw")) && checkUsersOnline(storeInput[1])==false){
- System.out.println("logged in");
- usersOnline.add(storeInput[1]);
- cabinNo = storeInput[1];
- }
- else{
- System.out.println("user already online");
- }
- }
- }
- break;
- case "3":
- System.out.println(cabinNo);
- break;
- }
- }
- catch(SQLException e){
- System.out.println("SQL exception");
- }
- catch(ClassNotFoundException e){
- System.out.println("Class not found exception");
- }
- catch(ArrayIndexOutOfBoundsException e){
- System.out.println("Array Index out of bounds");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement