Advertisement
reage

Untitled

Jan 3rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.90 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.net.*;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.ResultSetMetaData;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.Properties;
  15.  
  16.  
  17. public class ServerTCPThread extends Thread {
  18.     Socket mySocket;
  19.  
  20.     public ServerTCPThread(Socket socket) {
  21.         super(); // konstruktor klasy Thread
  22.         mySocket = socket;
  23.     }
  24.  
  25.    
  26.     public void run() // program wątku
  27.     {
  28.         try {
  29.            
  30.             if (checkDriver("com.mysql.jdbc.Driver"))
  31.                 System.out.println(" ... OK");
  32.             else
  33.                 System.exit(1);
  34.             Connection con = getConnection("jdbc:mysql://", "localhost", 3306, "root", "");
  35.             Statement st = createStatement(con);
  36.             Statement st2 = createStatement(con);
  37.             Statement st3 = createStatement(con);
  38.             // próba wybrania bazy
  39.             if (executeUpdate(st, "USE java;") == 0)
  40.                 System.out.println("Baza wybrana");
  41.             else
  42.                 System.out.println("Baza nie istnieje!");
  43.            
  44.             String sql = null;
  45.             String sql2 = null;
  46.             String sql3 = null;
  47.            
  48.             ResultSet result = null;
  49.             ResultSet result2 = null;
  50.             ResultSet result3 = null;
  51.            
  52.             BufferedReader in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
  53.             PrintWriter out = new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()));
  54.            
  55.             while(true)
  56.             {
  57.             String str = in.readLine();
  58.            
  59.             if(str.equals("workerLog")) {
  60.                 String id = in.readLine();
  61.                 String pass = in.readLine();
  62.                 boolean ans = false;
  63.                
  64.                 sql = "select login, password from employee";
  65.                 result = executeQuery(st, sql);
  66.                
  67.                 while(result.next())
  68.                 {
  69.                     if(id.equals(result.getString("login")) && pass.equals(result.getString("password")))
  70.                         ans = true;
  71.                 }
  72.                
  73.                 if(ans)
  74.                 {
  75.                     out.println("correct");
  76.                     out.flush();
  77.                     sql = "select first_name from employee where login='"+id+"'";
  78.                     result = executeQuery(st, sql);
  79.                     result.next();
  80.                     out.println(result.getString("first_name"));
  81.                     out.flush();
  82.                 }
  83.                 else {
  84.                     out.println("incorrect");
  85.                     out.flush();
  86.                 }
  87.             }
  88.            
  89.             if(str.equals("studentLog")) {
  90.                 String id = in.readLine();
  91.                 String pass = in.readLine();
  92.                 boolean ans = false;
  93.                
  94.                 sql = "select login, password from student";
  95.                 result = executeQuery(st, sql);
  96.                
  97.                 while(result.next())
  98.                 {
  99.                     if(id.equals(result.getString("login")) && pass.equals(result.getString("password")))
  100.                         ans = true;
  101.                 }
  102.                
  103.                 if(ans)
  104.                 {
  105.                     out.println("correct");
  106.                     out.flush();
  107.                     sql = "select first_name from student where login='"+id+"'";
  108.                     result = executeQuery(st, sql);
  109.                     result.next();
  110.                     out.println(result.getString("first_name"));
  111.                     out.flush();
  112.                 }
  113.                 else {
  114.                     out.println("incorrect");
  115.                     out.flush();
  116.                 }
  117.             }
  118.            
  119.             if(str.equals("searchStudent")) {
  120.                 String pesel = in.readLine();
  121.                 boolean ans = false;
  122.                
  123.                 sql = "select * from student where PESEL='"+pesel+"'";
  124.                 sql2 = "select * from signed_exams_t where PESEL='"+pesel+"'";
  125.                 sql3 = "select * from signed_exams where PESEL='"+pesel+"'";
  126.                
  127.                 result = executeQuery(st, sql);
  128.                 result2 = executeQuery(st2, sql2);
  129.                 result3 = executeQuery(st3, sql3);
  130.                
  131.                
  132.                 if(result.isBeforeFirst())
  133.                 {
  134.                     result.next();
  135.                     if(pesel.equals(result.getString("PESEL")))
  136.                         ans = true;
  137.                 }
  138.                 else
  139.                 {
  140.                     ans = false;
  141.                 }
  142.            
  143.                
  144.                 if(ans)
  145.                 {
  146.                     out.println("correct");
  147.                     out.flush();
  148.                     out.println(result.getString("first_name"));
  149.                     out.flush();
  150.                     out.println(result.getString("last_name"));
  151.                     out.flush();
  152.                     out.println(result.getString("address"));
  153.                     out.flush();
  154.                    
  155.                    
  156.                     if(Integer.parseInt(result.getString("osk_course_status")) == 1)
  157.                     {
  158.                         out.println("1");
  159.                         out.flush();
  160.                     }
  161.                     else
  162.                     {
  163.                         out.println("0");
  164.                         out.flush();
  165.                     }
  166.                     if(result2.next())
  167.                     {
  168.                             out.println(result2.getString("date"));
  169.                             out.flush();
  170.                             out.println(result2.getString("hour"));
  171.                             out.flush();
  172.                         if(result2.getString("score") == null)
  173.                             {
  174.                                 out.println("0");
  175.                                 out.flush();
  176.                             }
  177.                         else if(result2.getInt("score") < 92)
  178.                             {
  179.                                 out.println("1");
  180.                                 out.flush();
  181.                             }
  182.                         else
  183.                             {
  184.                                 out.println("2");
  185.                                 out.flush();
  186.                             }
  187.                     }
  188.                     else
  189.                     {
  190.                         out.println("0");
  191.                         out.flush();
  192.                     }
  193.                    
  194.                     if(result3.next())
  195.                     {
  196.                             out.println(result3.getString("date"));
  197.                             out.flush();
  198.                             out.println(result3.getString("hour"));
  199.                             out.flush();
  200.                     }
  201.                     else
  202.                     {
  203.                         out.println("0");
  204.                         out.flush();
  205.                     }
  206.                    
  207.                     if(Integer.parseInt(result.getString("pay_texam_status")) == 1)
  208.                     {
  209.                         out.println("1");
  210.                         out.flush();
  211.                     }
  212.                     else
  213.                     {
  214.                         out.println("0");
  215.                         out.flush();
  216.                     }
  217.                     if(Integer.parseInt(result.getString("pay_pexam_status")) == 1)
  218.                     {
  219.                         out.println("1");
  220.                         out.flush();
  221.                     }
  222.                     else
  223.                     {
  224.                         out.println("0");
  225.                         out.flush();
  226.                     }
  227.                    
  228.                 }
  229.                 else
  230.                 {
  231.                     out.println("incorrect");
  232.                     out.flush();
  233.                 }
  234.             }
  235.            
  236.             if(str.equals("searchExamPesel"))
  237.             {
  238.                 String pesel = in.readLine();
  239.                 boolean ans = false;
  240.                
  241.                 sql = "select * from signed_exams where pesel='"+pesel+"'";
  242.                 result = executeQuery(st, sql);
  243.                
  244.                 if(result.isBeforeFirst())
  245.                 {
  246.                     result.next();
  247.                     if(pesel.equals(result.getString("PESEL")))
  248.                         ans = true;
  249.                 }
  250.                 else
  251.                 {
  252.                     ans = false;
  253.                 }
  254.                
  255.                 if(ans)
  256.                 {
  257.                     out.println("correct");
  258.                     out.flush();
  259.                    
  260.                     out.println(result.getInt("id"));
  261.                     out.flush();
  262.                     out.println(result.getString("date"));
  263.                     out.flush();
  264.                     out.println(result.getLong("PESEL"));
  265.                     out.flush();
  266.                     out.println(result.getString("first_name"));
  267.                     out.flush();
  268.                     out.println(result.getString("last_name"));
  269.                     out.flush();
  270.                     out.println(result.getInt("vehicle_id"));
  271.                     out.flush();
  272.                     out.println(result.getInt("examiner_id"));
  273.                     out.flush();
  274.                 }
  275.                
  276.                 else
  277.                 {
  278.                     out.println("incorrect");
  279.                     out.flush();
  280.                 }
  281.                
  282.             }
  283.            
  284.             if(str.equals("searchExamAll"))
  285.             {
  286.                
  287.             }
  288.            
  289.             if(str.equals("searchExamFrom"))
  290.             {
  291.                
  292.             }
  293.             }
  294.             //mySocket.close();
  295.  
  296.         } catch (Exception e) {
  297.             System.err.println(e);
  298.         }
  299.     }
  300.  
  301.    
  302.     @SuppressWarnings("deprecation")
  303.     public static boolean checkDriver(String driver) {
  304.         // LADOWANIE STEROWNIKA
  305.         //System.out.print("Sprawdzanie sterownika:");
  306.         try {
  307.             Class.forName(driver).newInstance();
  308.             return true;
  309.         } catch (Exception e) {
  310.             System.out.println("Blad przy ladowaniu sterownika bazy!");
  311.             return false;
  312.         }
  313.     }
  314.    
  315.     public static Connection getConnection(String kindOfDatabase, String adres, int port, String userName, String password) {
  316.  
  317.         Connection conn = null;
  318.         Properties connectionProps = new Properties();
  319.         connectionProps.put("user", userName);
  320.         connectionProps.put("password", password);
  321.         try {
  322.             conn = DriverManager.getConnection(kindOfDatabase + adres + ":" + port + "/",
  323.                     connectionProps);
  324.         } catch (SQLException e) {
  325.             System.out.println("Błąd połączenia z bazą danych! " + e.getMessage() + ": " + e.getErrorCode());
  326.             System.exit(2);
  327.         }
  328.         //System.out.println("Połączenie z bazą danych: ... OK");
  329.         return conn;
  330.     }
  331.    
  332.    
  333.     private static Statement createStatement(Connection connection) {
  334.         try {
  335.             return connection.createStatement();
  336.         } catch (SQLException e) {
  337.             System.out.println("Błąd createStatement! " + e.getMessage() + ": " + e.getErrorCode());
  338.             System.exit(3);
  339.         }
  340.         return null;
  341.     }
  342.  
  343.    
  344.     @SuppressWarnings("unused")
  345.     private static void closeConnection(Connection connection, Statement s) {
  346.         System.out.print("\nZamykanie polaczenia z bazą:");
  347.         try {
  348.             s.close();
  349.             connection.close();
  350.         } catch (SQLException e) {
  351.             System.out
  352.                     .println("Bląd przy zamykaniu polączenia z bazą! " + e.getMessage() + ": " + e.getErrorCode());;
  353.             System.exit(4);
  354.         }
  355.         System.out.print(" zamknięcie OK");
  356.     }
  357.  
  358.    
  359.     private static ResultSet executeQuery(Statement s, String sql) {
  360.         try {
  361.             return s.executeQuery(sql);
  362.         } catch (SQLException e) {
  363.             System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
  364.         }
  365.         return null;
  366.     }
  367.    
  368.     private static int executeUpdate(Statement s, String sql) {
  369.         try {
  370.             return s.executeUpdate(sql);
  371.         } catch (SQLException e) {
  372.             System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
  373.         }
  374.         return -1;
  375.     }
  376.    
  377.    
  378.     @SuppressWarnings("unused")
  379.     private static void printDataFromQuery(ResultSet r) {
  380.         ResultSetMetaData rsmd;
  381.         try {
  382.             rsmd = r.getMetaData();
  383.             int numcols = rsmd.getColumnCount(); // pobieranie liczby kolumn
  384.             // wyswietlanie nazw kolumn:
  385.             for (int i = 1; i <= numcols; i++) {
  386.                 System.out.print("\t" + rsmd.getColumnLabel(i) + "\t|");
  387.             }
  388.             System.out
  389.                     .print("\n____________________________________________________________________________\n");
  390.            
  391.             // wyswietlanie kolejnych rekordow:
  392.             while (r.next()) {
  393.                 for (int i = 1; i <= numcols; i++) {
  394.                     Object obj = r.getObject(i);
  395.                     if (obj != null)
  396.                         System.out.print("\t" + obj.toString() + "\t|");
  397.                     else
  398.                         System.out.print("\t");
  399.                 }
  400.                 System.out.println();
  401.             }
  402.         } catch (SQLException e) {
  403.             System.out.println("Bląd odczytu z bazy! " + e.getMessage() + ": " + e.getErrorCode());
  404.         }
  405.     }
  406.    
  407.     public static void sqlGetDataByName(ResultSet r) {
  408.         System.out.println("Pobieranie danych z wykorzystaniem nazw kolumn");
  409.         try {
  410.             ResultSetMetaData rsmd = r.getMetaData();
  411.             int numcols = rsmd.getColumnCount();
  412.             // Tytul tabeli z etykietami kolumn zestawow wynikow
  413.             for (int i = 1; i <= numcols; i++) {
  414.                 System.out.print(rsmd.getColumnLabel(i) + "\t|\t");
  415.             }
  416.             System.out
  417.             .print("\n____________________________________________________________________________\n");
  418.             while (r.next()) {
  419.                 int size = r.getMetaData().getColumnCount();
  420.                 for(int i = 1; i <= size; i++){
  421.                     switch(r.getMetaData().getColumnTypeName(i)){
  422.                     case "INT":
  423.                         System.out.print(r.getInt(r.getMetaData().getColumnName(i)) + "\t|\t");
  424.                         break;
  425.                     case "DATE":
  426.                         System.out.print(r.getDate(r.getMetaData().getColumnName(i)) + "\t|\t");
  427.                         break;
  428.                     case "VARCHAR":
  429.                         System.out.print(r.getString(r.getMetaData().getColumnName(i)) + "\t|\t");
  430.                         break;
  431.                     default:
  432.                         System.out.print(r.getMetaData().getColumnTypeName(i));
  433.                     }
  434.                 }
  435.                 System.out.println();
  436.             }
  437.         } catch (SQLException e) {
  438.             System.out.println("Bląd odczytu z bazy! " + e.getMessage() + ": " + e.getErrorCode());
  439.         }
  440.     }
  441.    
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement