Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. public class NewConnection extends Thread
  2. {
  3. Socket s;
  4. int num;
  5.  
  6. NewConnection(int num, Socket s)
  7. {
  8. this.num = num;
  9. this.s = s;
  10.  
  11. setDaemon(true);
  12. setPriority(NORM_PRIORITY);
  13. start();
  14. System.out.println("Got client");
  15. }
  16.  
  17. public void run()
  18. {
  19. try
  20. {
  21. InputStream sin = s.getInputStream();
  22. OutputStream sout = s.getOutputStream();
  23.  
  24. // Конвертируем Stream'ы в другой тип, чтоб легче обрабатывать
  25. // текстовые сообщения.
  26. DataInputStream in = new DataInputStream(sin);
  27. DataOutputStream out = new DataOutputStream(sout);
  28.  
  29. String url = null;
  30. // Ожидаем, пока клиент пришлёт строку текста.
  31. while (true)
  32. {
  33. url = "jdbc:mysql://localhost:3306/Clients";
  34. System.out.println("sending url");
  35. out.flush(); // Заставляем Stream закончить передачу данных.
  36. System.out.println("Waiting for the next line...");
  37. System.out.println();
  38. }
  39. }
  40. catch (Exception x)
  41. {
  42. x.printStackTrace();
  43. }
  44. }
  45. }
  46.  
  47. connect.addActionListener(new ActionListener()
  48. {
  49. public void actionPerformed(ActionEvent e)
  50. {
  51. thread = new Thread(new Runnable()
  52. {
  53. public void run()
  54. {
  55. int serverPort = 6666;
  56. String address = "127.0.0.1";
  57. try
  58. {
  59. InetAddress ipAddress = InetAddress.getByName(address);
  60. socket = new Socket(ipAddress, serverPort);
  61. InputStream sin = socket.getInputStream();
  62. OutputStream sout = socket.getOutputStream();
  63. // Конвертируем потоки в другой тип, чтоб легче
  64. // обрабатывать текстовые сообщения.
  65. DataInputStream in = new DataInputStream(sin);
  66. DataOutputStream out = new DataOutputStream(sout);
  67. System.out.println("You are connected to " + address
  68. + " and port " + serverPort);
  69. connected = true;
  70. }
  71. catch (Exception x)
  72. {
  73. // errorFrameFunc();
  74. new Error("Connection Error", "Server is down");
  75. }
  76. // // Создаем поток для чтения с клавиатуры.
  77. // BufferedReader keyboard = new BufferedReader(new
  78. // InputStreamReader(System.in));
  79. // String line = null;
  80. // System.out.println("Type in something and press
  81. // enter. Will send it to the server and tell ya what it
  82. // thinks.");
  83. // System.out.println();
  84.  
  85. // while (true) {
  86. // /*line = keyboard.readLine(); // ждем пока
  87. // пользователь введет что-то и нажмет кнопку Enter.
  88. // System.out.println("Sending this line to the
  89. // server...");
  90. // out.writeUTF(line); // отсылаем введенную строку
  91. // текста серверу.*/
  92. //// out.flush(); // заставляем поток закончить передачу
  93. // данных.
  94. //// line = in.readUTF(); // ждем пока сервер отошлет
  95. // строку текста.
  96. // }
  97. }
  98. });
  99. thread.setDaemon(true);
  100. thread.start();
  101. }
  102.  
  103. });
  104.  
  105. oK.addActionListener(new ActionListener()
  106. {
  107. public void actionPerformed(ActionEvent e)
  108. {
  109. if (groupTextField.getText().equals("") || nameTextField.getText().equals("")
  110. || surnameTextField.getText().equals("") || passwordTextField.getPassword().length == 0)
  111. new Error("Empty error", "One or all fields is empty,please enter the information");
  112. else
  113. {
  114. if (!Arrays.equals(passwordTextField.getPassword(), passwordConTextField.getPassword()))
  115. new Error("Password Error", "Passwords do not match, enter the same password");
  116. else
  117. {
  118. String queryCheck = "select Groups,Name,Surname from clients";
  119. String query = "INSERT INTO Clients.clients(Groups,Name,Surname,Password) " + " VALUES ('"
  120. + groupTextField.getText() + "','" + nameTextField.getText() + "','"
  121. + surnameTextField.getText() + "','" + new String(passwordTextField.getPassword())
  122. + "');";
  123. try
  124. {
  125. // opening database connection to MySQL server
  126. con = DriverManager.getConnection(url, user, passwordForRoot);
  127. // getting Statement object to execute query
  128. stmt = con.createStatement();
  129. // executing SELECT query
  130. rs = stmt.executeQuery(queryCheck);
  131. while (rs.next())
  132. {
  133. if (rs.getString(1).equals(groupTextField.getText())
  134. && rs.getString(2).equals(nameTextField.getText())
  135. && rs.getString(3).equals(surnameTextField.getText()))
  136. {
  137. new Error("Row error", "The same account is already exist");
  138. break;
  139. }
  140. }
  141.  
  142. stmt.executeUpdate(query);
  143. }
  144. catch (SQLException sqlEx)
  145. {
  146. sqlEx.printStackTrace();
  147. }
  148. finally
  149. {
  150. // close connection ,stmt and resultset here
  151. try
  152. {
  153. con.close();
  154. }
  155. catch (SQLException se)
  156. {
  157. /* can't do anything */ }
  158. try
  159. {
  160. stmt.close();
  161. }
  162. catch (SQLException se)
  163. {
  164. /* can't do anything */ }
  165. try
  166. {
  167. rs.close();
  168. }
  169. catch (SQLException se)
  170. {
  171. /* can't do anything */ }
  172. }
  173. }
  174. }
  175.  
  176. }
  177. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement