Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. public class NewConnection extends Thread {
  2. Socket s;
  3. int num;
  4.  
  5. NewConnection(int num, Socket s) {
  6. this.num = num;
  7. this.s = s;
  8.  
  9. setDaemon(true);
  10. setPriority(NORM_PRIORITY);
  11. start();
  12. System.out.println("Got client");
  13. }
  14.  
  15. public void run() {
  16. try {
  17. InputStream sin = s.getInputStream();
  18. OutputStream sout = s.getOutputStream();
  19.  
  20. // Конвертируем потоки в другой тип, чтоб легче обрабатывать текстовые сообщения.
  21. DataInputStream in = new DataInputStream(sin);
  22. DataOutputStream out = new DataOutputStream(sout);
  23.  
  24. String url = null;
  25. while (true) {
  26. url = "jdbc:mysql://localhost:3306/Clients"; // ожидаем пока клиент пришлет строку текста.
  27. System.out.println("sending url");
  28. out.flush(); // заставляем поток закончить передачу данных.
  29. System.out.println("Waiting for the next line...");
  30. System.out.println();
  31. }
  32. }catch (Exception x)
  33. {
  34. x.printStackTrace();
  35. }
  36. }
  37.  
  38. connect.addActionListener(new ActionListener() {
  39. public void actionPerformed(ActionEvent e) {
  40.  
  41. thread = new Thread(new Runnable() {
  42. public void run() {
  43. int serverPort = 6666;
  44. String address = "127.0.0.1";
  45. try {
  46. InetAddress ipAddress = InetAddress.getByName(address);
  47. socket = new Socket(ipAddress, serverPort);
  48.  
  49. InputStream sin = socket.getInputStream();
  50. OutputStream sout = socket.getOutputStream();
  51. // Конвертируем потоки в другой тип, чтоб легче обрабатывать текстовые сообщения.
  52. DataInputStream in = new DataInputStream(sin);
  53. DataOutputStream out = new DataOutputStream(sout);
  54. System.out.println("You are connected to " +
  55. address + " and port " + serverPort);
  56. connected = true;
  57. } catch (Exception x) {
  58.  
  59. });
  60.  
  61. oK.addActionListener(new ActionListener() {
  62. public void actionPerformed(ActionEvent e) {
  63. if(groupTextField.getText().equals("")||nameTextField.getText().equals("")
  64. ||surnameTextField.getText().equals("")
  65. ||passwordTextField.getPassword().length == 0)
  66. new Error("Empty error","One or all fields is empty,please enter the information");
  67. else {
  68. if (!Arrays.equals(passwordTextField.getPassword(), passwordConTextField.getPassword()))
  69. new Error("Password Error","Passwords do not match, enter the same password");
  70. else {
  71. String queryCheck = "select Groups,Name,Surname from clients";
  72. String query = "INSERT INTO Clients.clients(Groups,Name,Surname,Password) " +
  73. " VALUES ('" + groupTextField.getText() + "','" + nameTextField.getText()
  74. + "','" + surnameTextField.getText() + "','" + new String(passwordTextField.getPassword()) + "');";
  75. try {
  76. // opening database connection to MySQL server
  77. con = DriverManager.getConnection(url, user, passwordForRoot);
  78. // getting Statement object to execute query
  79. stmt = con.createStatement();
  80. // executing SELECT query
  81. rs = stmt.executeQuery(queryCheck);
  82. while(rs.next())
  83. {
  84. if(rs.getString(1).equals(groupTextField.getText()) && rs.getString(2).equals(nameTextField.getText())
  85. && rs.getString(3).equals(surnameTextField.getText()))
  86. {
  87. new Error("Row error","The same account is already exist");
  88. break;
  89. }
  90. }
  91.  
  92. stmt.executeUpdate(query);
  93. } catch (SQLException sqlEx) {
  94. sqlEx.printStackTrace();
  95. } finally {
  96. //close connection ,stmt and resultset here
  97. try {
  98. con.close();
  99. } catch (SQLException se) { /*can't do anything */ }
  100. try {
  101. stmt.close();
  102. } catch (SQLException se) { /*can't do anything */ }
  103. try {
  104. rs.close();
  105. } catch (SQLException se) { /*can't do anything */ }
  106. }
  107. }
  108. }
  109.  
  110. }
  111. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement