Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- //not yet threaded!!
- public class Server {
- private static Connection connection;
- private static HashMap<Integer, Student> studentData;
- private static HashMap<Integer, Course> courseData;
- private static HashMap<Integer, Enrollment> enrollmentData; //maps of all the data from the server tables
- public static void main(String[] args) {
- try {
- connectToDB();
- studentData = mapStudentData();
- courseData = mapCourseData();
- enrollmentData = mapEnrollmentData(); //maps with asigned values
- ServerSocket server = new ServerSocket(5678);
- while(true) {
- Socket client = server.accept(); //looking for the client connection
- ObjectInputStream input = new ObjectInputStream(client.getInputStream());
- ObjectOutputStream output = new ObjectOutputStream(client.getOutputStream());
- Request request = (Request) input.readObject(); //recieving the request from the client
- String requestType = request.getRequestType();
- String studId = request.getStudetnId();
- String courseId = request.getCourseId();
- // for sending info back to the client
- if(requestType.equals("Student")) {
- ArrayList<Student> studentToClient = processStudentRequest(studId, courseId);
- output.writeObject(studentToClient.toArray(new Student[studentToClient.size()]));
- } else if(requestType.equals("Course")) {
- } else {
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- //for when the detected request type is student
- public static ArrayList<Student> processStudentRequest(String studId, String courseId) {
- ArrayList<Student> students = new ArrayList<Student>();
- if(courseId.length() == 0) { //if course is empty
- //just gets all students and adds them to the list
- for(Integer i : studentData.keySet()) {
- Student s = studentData.get(i);
- students.add(s);
- }
- return students;
- //course is filled (not yet handling improper inputs
- } else if(courseId.length() != 0) {
- ArrayList<Integer> ssn = new ArrayList<Integer>();
- //adds all ssns from enrolment that match the course id
- for(Integer i : enrollmentData.keySet()) {
- Enrollment e = enrollmentData.get(i);
- if(e.getCourseId().equals(courseId)) {
- ssn.add(e.getSsn());
- }
- }
- //looks the students for the found ssns
- for(Integer i : studentData.keySet()) {
- Student s = studentData.get(i);
- for(int j = 0; j < ssn.size(); j++) {
- if(ssn.get(j) == s.getSsn()) {
- students.add(s);
- }
- }
- }
- return students;
- } else {
- //might not be necesary now that im looking at it
- return null;
- }
- }
- public static void connectToDB() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver Loaded");
- String host = "jdbc:mysql://18.191.16.207/ddherman";
- String uName = "ddherman";
- String uPass = "cs357";
- connection = DriverManager.getConnection(host, uName, uPass);
- System.out.println("Connection Succesful");
- } catch (Exception ex) {
- //System.out.println("Connection Failed");
- ex.printStackTrace();
- }
- }
- //next three methods just add all the data from each table
- //to a map.
- public static HashMap<Integer, Student> mapStudentData() {
- try {
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM Student");
- HashMap<Integer, Student> studentMap = new HashMap<Integer, Student>();
- Student studentData;
- while(rs.next()) {
- Integer ssn = rs.getInt("ssn");
- String firstName = rs.getString("firstName");
- String mi = rs.getString("mi");
- String lastName = rs.getString("lastName");
- String phone = rs.getString("phone");
- String birthDate = rs.getString("birthDate");
- String street = rs.getString("street");
- int zipCode = rs.getInt("zipCode");
- String deptId = rs.getString("deptId");
- studentData = new Student(ssn, firstName, mi, lastName, phone, birthDate,
- street, zipCode, deptId);
- studentMap.put(ssn, studentData);
- }
- return studentMap;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return null;
- }
- public static HashMap<Integer, Course> mapCourseData() {
- try {
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM Course");
- HashMap<Integer, Course> courseMap = new HashMap<Integer, Course>();
- Course courseData;
- while(rs.next()) {
- Integer courseId = rs.getInt("courseId");
- String subjectId = rs.getString("subjectId");
- int courseNumber = rs.getInt("courseNumber");
- String title = rs.getString("title");
- int numOfCredits = rs.getInt("numOfCredits");
- courseData = new Course(courseId, subjectId, courseNumber, title, numOfCredits);
- courseMap.put(courseId, courseData);
- }
- return courseMap;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return null;
- }
- public static HashMap<Integer, Enrollment> mapEnrollmentData() {
- try {
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM Enrollment");
- HashMap<Integer, Enrollment> enrollmentMap = new HashMap<Integer, Enrollment>();
- Enrollment enrollmentData;
- while(rs.next()) {
- Integer ssn = rs.getInt("ssn");
- String courseId = rs.getString("courseId");
- String dateRegistered = rs.getString("dateRegistered");
- String grade = rs.getString("grade");
- enrollmentData = new Enrollment(ssn, courseId, dateRegistered, grade);
- enrollmentMap.put(ssn, enrollmentData);
- }
- return enrollmentMap;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment