Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Font;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ItemEvent;
  8. import java.awt.event.ItemListener;
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.PrintWriter;
  13. import java.net.ServerSocket;
  14. import java.net.Socket;
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.text.ParseException;
  21. import java.util.*;
  22. import javax.swing.JFrame;
  23. import javax.swing.JLabel;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.JPanel;
  26. import javax.swing.ButtonGroup;
  27. import javax.swing.JButton;
  28. import javax.swing.JRadioButton;
  29. import javax.swing.JTextArea;
  30. import javax.swing.JTextField;
  31. import javax.swing.border.LineBorder;
  32.  
  33.  
  34. class Server extends Thread
  35. {
  36. static int port = 5000;
  37. String address = "127.0.0.1";
  38. static ServerSocket ss;
  39.  
  40. @Override
  41. public void run()
  42. {
  43. BufferedReader in = null;
  44. PrintWriter out = null;
  45. Connection con = null;
  46. PreparedStatement stmt = null;
  47. Socket s = null;
  48.  
  49. while(true)
  50. {
  51. try
  52. {
  53. System.out.println("poczatek");
  54. if(!ss.isBound())
  55. {
  56. s = ss.accept();
  57. }
  58. else
  59. {
  60. Server thread2 = new Server();
  61. thread2.start();
  62. }
  63. System.out.println("polaczylem sie z klientem");
  64. in = new BufferedReader(new InputStreamReader(s.getInputStream())); //wejsciowy
  65. out = new PrintWriter(s.getOutputStream(), true); //wyjsciowy
  66. String line = in.readLine();
  67.  
  68. if(line.matches("\\d.*")) // sygnal bazodanowy
  69. {
  70. System.out.println("Odebralem działanie: "+line);
  71. if(line.startsWith("1"))
  72. {
  73. line = line.substring(1,line.length());
  74. String login = line.split(" ")[0];
  75. String haslo = line.split(" ")[1];
  76. System.out.println("login: "+ login +" haslo: " + haslo);
  77.  
  78. try
  79. {
  80. System.out.println("lalala");
  81. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/komunikator","root","local");
  82. stmt = con.prepareStatement("INSERT INTO user(login,password) VALUES (?,?)");
  83. stmt.setString(1, login);
  84. stmt.setString(2, haslo);
  85. stmt.executeUpdate();
  86. out.println("1created");
  87.  
  88. }
  89. catch (SQLException ex)
  90. {
  91. if (ex.getSQLState().startsWith("23"))
  92. {
  93. out.println("1error1");
  94. }
  95. else
  96. {
  97. out.println("1error2");
  98. }
  99. ex.printStackTrace();
  100. }
  101. finally
  102. { // zwalnianie zasobow
  103. if (stmt != null)
  104. {
  105. try
  106. {
  107. stmt.close();
  108. }
  109. catch (SQLException sqlEx) { }
  110. stmt = null;
  111. }
  112. if(con != null)
  113. {
  114. try
  115. {
  116. con.close();
  117. }
  118. catch (SQLException sqlEx) { }
  119. con = null;
  120. }
  121. }
  122.  
  123. }
  124. else if(line.startsWith("2"))
  125. {
  126.  
  127. }
  128. }
  129. else
  130. {
  131. out.println("Serwer nie obsluguje "+line);
  132. System.out.println("Dostalem: "+line +" i to nie jest obsługiwane dzialanie");
  133. }
  134. }
  135. catch(Exception e)
  136. {
  137. System.out.println("Problem z połączeniem TCP");
  138. e.printStackTrace();
  139. System.exit(1);
  140. }
  141. }
  142. }
  143.  
  144. public static void main(String [] args)
  145. {
  146. try
  147. {
  148. ss = new ServerSocket(Integer.valueOf(port));
  149.  
  150. Server thread1 = new Server();
  151. thread1.start();
  152. }
  153. catch(Exception e)
  154. {
  155. System.out.println("Problem z socketem serwera");
  156. e.printStackTrace();
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement