Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. public static void main(String [] args)
  2. {
  3. try
  4. {
  5. ss = new ServerSocket(Integer.valueOf(port));
  6. Thread t1 = new Thread()
  7. {
  8. @Override
  9. public void run()
  10. {
  11. BufferedReader in = null;
  12. PrintWriter out = null;
  13. Connection con = null;
  14. PreparedStatement stmt = null;
  15.  
  16. while(true)
  17. {
  18.  
  19. try
  20. {
  21. System.out.println("poczatek");
  22.  
  23. Socket s = ss.accept();
  24. System.out.println("polaczylem sie z klientem");
  25. in = new BufferedReader(new InputStreamReader(s.getInputStream())); //wejsciowy
  26. out = new PrintWriter(s.getOutputStream(), true); //wyjsciowy
  27. String line = in.readLine();
  28.  
  29.  
  30. if(line.matches("\\d.*")) // sygnal bazodanowy
  31. {
  32. System.out.println("Odebralem działanie: "+line);
  33. if(line.startsWith("1"))
  34. {
  35. line = line.substring(1,line.length());
  36. String login = line.split(" ")[0];
  37. String haslo = line.split(" ")[1];
  38. System.out.println("login: "+ login +" haslo: " + haslo);
  39.  
  40. try
  41. {
  42. System.out.println("lalala");
  43. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/komunikator","root","local");
  44. stmt = con.prepareStatement("INSERT INTO user(login,password) VALUES (?,?)");
  45. stmt.setString(1, login);
  46. stmt.setString(2, haslo);
  47. stmt.executeUpdate();
  48. out.println("1created");
  49.  
  50. }
  51. catch (SQLException ex)
  52. {
  53. if (ex.getSQLState().startsWith("23"))
  54. {
  55. out.println("1error1");
  56. }
  57. else
  58. {
  59. out.println("1error2");
  60. }
  61. ex.printStackTrace();
  62. }
  63. finally
  64. { // zwalnianie zasobow
  65. if (stmt != null)
  66. {
  67. try
  68. {
  69. stmt.close();
  70. }
  71. catch (SQLException sqlEx) { }
  72. stmt = null;
  73. }
  74. if(con != null)
  75. {
  76. try
  77. {
  78. con.close();
  79. }
  80. catch (SQLException sqlEx) { }
  81. con = null;
  82. }
  83. }
  84.  
  85. }
  86. else if(line.startsWith("2"))
  87. {
  88.  
  89. }
  90.  
  91.  
  92. }
  93. else
  94. { out.println("Serwer nie obsluguje "+line);
  95. System.out.println("Dostalem: "+line +" i to nie jest obsługiwane dzialanie");
  96. }
  97. }
  98. catch(Exception e)
  99. {
  100. System.out.println("Problem z połączeniem TCP");
  101. e.printStackTrace();
  102. System.exit(1);
  103. }
  104.  
  105.  
  106. }
  107. }
  108. };
  109. t1.start();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement