Advertisement
Guest User

Untitled

a guest
May 23rd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1.  
  2. /// baza danych ///
  3. public synchronized String pobierzStatystyki(){
  4. Connection c = null;
  5. Statement stat = null;
  6. String wynik = "";
  7.  
  8. try {
  9. Class.forName("org.sqlite.JDBC");
  10.  
  11. c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
  12.  
  13. stat = c.createStatement();
  14.  
  15. String comenda = "SELECT * FROM STATYSTYKI";
  16.  
  17. ResultSet result = stat.executeQuery(comenda);
  18.  
  19. while(result.next()){
  20. wynik += result.getString("UZYTKOWNIK");
  21. wynik += "\n";
  22. wynik += result.getInt("PUNKTY");
  23. wynik += "\n";
  24. }
  25. stat.close();
  26. c.close();
  27.  
  28. } catch (ClassNotFoundException ex) {
  29. ex.printStackTrace();
  30. } catch (SQLException ex) {
  31. ex.printStackTrace();
  32. }
  33.  
  34. return wynik;
  35. }
  36.  
  37. public synchronized void dodajStatystyke(String nazwa,int punkty){
  38. Connection c = null;
  39. Statement stat = null;
  40.  
  41. try {
  42. Class.forName("org.sqlite.JDBC");
  43.  
  44. c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
  45.  
  46. stat = c.createStatement();
  47.  
  48. String comenda;
  49. /*comenda = "DROP TABLE STATYSTYKI";
  50. try{
  51. stat.executeUpdate(comenda);
  52. }catch(Exception ex){
  53.  
  54. }*/
  55. try{
  56. comenda = "CREATE TABLE STATYSTYKI("
  57. + "UZYTKOWNIK TEXT,"
  58. + "PUNKTY INTEGER"
  59. + ");";
  60.  
  61. stat.executeUpdate(comenda);
  62. }catch(Exception ex){
  63. //ex.printStackTrace();
  64. }
  65.  
  66. comenda = "INSERT INTO STATYSTYKI VALUES ("
  67. + "'"+ nazwa + "',"
  68. + punkty
  69. + ");";
  70.  
  71. stat.executeUpdate(comenda);
  72.  
  73.  
  74. stat.close();
  75. c.close();
  76.  
  77. } catch (ClassNotFoundException ex) {
  78. ex.printStackTrace();
  79. } catch (SQLException ex) {
  80. ex.printStackTrace();
  81. }
  82. }
  83.  
  84. public synchronized void czyscTabele() {
  85. Connection c = null;
  86. Statement stat = null;
  87.  
  88. try {
  89. Class.forName("org.sqlite.JDBC");
  90.  
  91. c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
  92.  
  93. stat = c.createStatement();
  94.  
  95. String comenda;
  96. comenda = "DROP TABLE STATYSTYKI";
  97. try{
  98. stat.executeUpdate(comenda);
  99. }catch(Exception ex){
  100.  
  101. }
  102. try{
  103. comenda = "CREATE TABLE STATYSTYKI("
  104. + "UZYTKOWNIK TEXT,"
  105. + "PUNKTY INTEGER"
  106. + ");";
  107.  
  108. stat.executeUpdate(comenda);
  109. }catch(Exception ex){
  110. //ex.printStackTrace();
  111. }
  112.  
  113.  
  114. stat.close();
  115. c.close();
  116. System.out.println("Tabela wyczyszczona");
  117. } catch (ClassNotFoundException ex) {
  118. ex.printStackTrace();
  119. } catch (SQLException ex) {
  120. ex.printStackTrace();
  121. }
  122. }
  123. }
  124. //////
  125.  
  126.  
  127.  
  128. public void run(){
  129. boolean uruchomiony = true;
  130.  
  131. try{
  132. while(uruchomiony){
  133. String message = (String) input.readObject();
  134.  
  135. System.out.println("Klient id="+id+" wiadomosc: " + message + " Data: "+new Date().toString());
  136. if(message.equals("koniec")){
  137. break;
  138. }else if(message.equals("wyslij na serwer")){
  139. //output.writeObject("Nazwa uzytkownika");
  140. String nazwaUzytkownika = (String) input.readObject();
  141. //output.writeObject("Liczba punktow");
  142. int punkty = (int) input.readObject();
  143. serwer.dodajStatystyke(nazwaUzytkownika, punkty);
  144. }else if(message.equals("pobierz statystyki")){
  145. output.writeObject(serwer.pobierzStatystyki());
  146. }
  147. }
  148. }catch(Exception ex){
  149.  
  150. }finally{
  151. System.out.println("Rozlaczono z klientem id=" +id );
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement