Guest User

Server

a guest
Oct 6th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. import java.io.ByteArrayOutputStream;
  2. import java.io.ObjectInputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13.  
  14. //not yet threaded!!
  15. public class Server {
  16. private static Connection connection;
  17.  
  18. private static HashMap<Integer, Student> studentData;
  19. private static HashMap<Integer, Course> courseData;
  20. private static HashMap<Integer, Enrollment> enrollmentData; //maps of all the data from the server tables
  21.  
  22. public static void main(String[] args) {
  23. try {
  24.  
  25. connectToDB();
  26. studentData = mapStudentData();
  27. courseData = mapCourseData();
  28. enrollmentData = mapEnrollmentData(); //maps with asigned values
  29.  
  30. ServerSocket server = new ServerSocket(5678);
  31.  
  32. while(true) {
  33. Socket client = server.accept(); //looking for the client connection
  34.  
  35. ObjectInputStream input = new ObjectInputStream(client.getInputStream());
  36. ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
  37. Request request = (Request) input.readObject(); //recieving the request from the client
  38.  
  39. String requestType = request.getRequestType();
  40. String studId = request.getStudetnId();
  41. String courseId = request.getCourseId();
  42.  
  43. // for sending info back to the client
  44. if(requestType.equals("Student")) {
  45. ArrayList<Student> studentToClient = processStudentRequest(studId, courseId);
  46. output.writeObject(studentToClient.toArray(new Student[studentToClient.size()]));
  47. } else if(requestType.equals("Course")) {
  48.  
  49. } else {
  50.  
  51. }
  52. }
  53.  
  54. } catch (Exception ex) {
  55. ex.printStackTrace();
  56. }
  57. }
  58.  
  59. //for when the detected request type is student
  60. public static ArrayList<Student> processStudentRequest(String studId, String courseId) {
  61. ArrayList<Student> students = new ArrayList<Student>();
  62. if(courseId.length() == 0) { //if course is empty
  63.  
  64. //just gets all students and adds them to the list
  65. for(Integer i : studentData.keySet()) {
  66. Student s = studentData.get(i);
  67.  
  68. students.add(s);
  69. }
  70. return students;
  71.  
  72. //course is filled (not yet handling improper inputs
  73. } else if(courseId.length() != 0) {
  74. ArrayList<Integer> ssn = new ArrayList<Integer>();
  75.  
  76. //adds all ssns from enrolment that match the course id
  77. for(Integer i : enrollmentData.keySet()) {
  78. Enrollment e = enrollmentData.get(i);
  79. if(e.getCourseId().equals(courseId)) {
  80. ssn.add(e.getSsn());
  81. }
  82. }
  83.  
  84. //looks the students for the found ssns
  85. for(Integer i : studentData.keySet()) {
  86. Student s = studentData.get(i);
  87.  
  88. for(int j = 0; j < ssn.size(); j++) {
  89. if(ssn.get(j) == s.getSsn()) {
  90. students.add(s);
  91. }
  92. }
  93. }
  94.  
  95. return students;
  96. } else {
  97. //might not be necesary now that im looking at it
  98. return null;
  99. }
  100. }
  101.  
  102. public static void connectToDB() {
  103. try {
  104. Class.forName("com.mysql.jdbc.Driver");
  105. System.out.println("Driver Loaded");
  106.  
  107. String host = "jdbc:mysql://18.191.16.207/ddherman";
  108. String uName = "ddherman";
  109. String uPass = "cs357";
  110.  
  111. connection = DriverManager.getConnection(host, uName, uPass);
  112. System.out.println("Connection Succesful");
  113.  
  114. } catch (Exception ex) {
  115. //System.out.println("Connection Failed");
  116. ex.printStackTrace();
  117. }
  118. }
  119.  
  120. //next three methods just add all the data from each table
  121. //to a map.
  122. public static HashMap<Integer, Student> mapStudentData() {
  123. try {
  124. Statement st = connection.createStatement();
  125. ResultSet rs = st.executeQuery("SELECT * FROM Student");
  126.  
  127. HashMap<Integer, Student> studentMap = new HashMap<Integer, Student>();
  128. Student studentData;
  129.  
  130. while(rs.next()) {
  131. Integer ssn = rs.getInt("ssn");
  132. String firstName = rs.getString("firstName");
  133. String mi = rs.getString("mi");
  134. String lastName = rs.getString("lastName");
  135. String phone = rs.getString("phone");
  136. String birthDate = rs.getString("birthDate");
  137. String street = rs.getString("street");
  138. int zipCode = rs.getInt("zipCode");
  139. String deptId = rs.getString("deptId");
  140.  
  141. studentData = new Student(ssn, firstName, mi, lastName, phone, birthDate,
  142. street, zipCode, deptId);
  143.  
  144. studentMap.put(ssn, studentData);
  145. }
  146.  
  147. return studentMap;
  148.  
  149. } catch (SQLException ex) {
  150. ex.printStackTrace();
  151. }
  152.  
  153. return null;
  154. }
  155.  
  156. public static HashMap<Integer, Course> mapCourseData() {
  157. try {
  158. Statement st = connection.createStatement();
  159. ResultSet rs = st.executeQuery("SELECT * FROM Course");
  160.  
  161. HashMap<Integer, Course> courseMap = new HashMap<Integer, Course>();
  162. Course courseData;
  163.  
  164. while(rs.next()) {
  165. Integer courseId = rs.getInt("courseId");
  166. String subjectId = rs.getString("subjectId");
  167. int courseNumber = rs.getInt("courseNumber");
  168. String title = rs.getString("title");
  169. int numOfCredits = rs.getInt("numOfCredits");
  170.  
  171. courseData = new Course(courseId, subjectId, courseNumber, title, numOfCredits);
  172.  
  173. courseMap.put(courseId, courseData);
  174. }
  175.  
  176. return courseMap;
  177.  
  178. } catch (SQLException ex) {
  179. ex.printStackTrace();
  180. }
  181.  
  182. return null;
  183. }
  184.  
  185. public static HashMap<Integer, Enrollment> mapEnrollmentData() {
  186. try {
  187. Statement st = connection.createStatement();
  188. ResultSet rs = st.executeQuery("SELECT * FROM Enrollment");
  189.  
  190. HashMap<Integer, Enrollment> enrollmentMap = new HashMap<Integer, Enrollment>();
  191. Enrollment enrollmentData;
  192.  
  193. while(rs.next()) {
  194. Integer ssn = rs.getInt("ssn");
  195. String courseId = rs.getString("courseId");
  196. String dateRegistered = rs.getString("dateRegistered");
  197. String grade = rs.getString("grade");
  198.  
  199. enrollmentData = new Enrollment(ssn, courseId, dateRegistered, grade);
  200.  
  201. enrollmentMap.put(ssn, enrollmentData);
  202. }
  203.  
  204. return enrollmentMap;
  205.  
  206. } catch (SQLException ex) {
  207. ex.printStackTrace();
  208. }
  209.  
  210. return null;
  211. }
  212.  
  213. }
Add Comment
Please, Sign In to add comment