Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// baza danych ///
- public synchronized String pobierzStatystyki(){
- Connection c = null;
- Statement stat = null;
- String wynik = "";
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
- stat = c.createStatement();
- String comenda = "SELECT * FROM STATYSTYKI";
- ResultSet result = stat.executeQuery(comenda);
- while(result.next()){
- wynik += result.getString("UZYTKOWNIK");
- wynik += "\n";
- wynik += result.getInt("PUNKTY");
- wynik += "\n";
- }
- stat.close();
- c.close();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return wynik;
- }
- public synchronized void dodajStatystyke(String nazwa,int punkty){
- Connection c = null;
- Statement stat = null;
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
- stat = c.createStatement();
- String comenda;
- /*comenda = "DROP TABLE STATYSTYKI";
- try{
- stat.executeUpdate(comenda);
- }catch(Exception ex){
- }*/
- try{
- comenda = "CREATE TABLE STATYSTYKI("
- + "UZYTKOWNIK TEXT,"
- + "PUNKTY INTEGER"
- + ");";
- stat.executeUpdate(comenda);
- }catch(Exception ex){
- //ex.printStackTrace();
- }
- comenda = "INSERT INTO STATYSTYKI VALUES ("
- + "'"+ nazwa + "',"
- + punkty
- + ");";
- stat.executeUpdate(comenda);
- stat.close();
- c.close();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- public synchronized void czyscTabele() {
- Connection c = null;
- Statement stat = null;
- try {
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:statystyki.db");
- stat = c.createStatement();
- String comenda;
- comenda = "DROP TABLE STATYSTYKI";
- try{
- stat.executeUpdate(comenda);
- }catch(Exception ex){
- }
- try{
- comenda = "CREATE TABLE STATYSTYKI("
- + "UZYTKOWNIK TEXT,"
- + "PUNKTY INTEGER"
- + ");";
- stat.executeUpdate(comenda);
- }catch(Exception ex){
- //ex.printStackTrace();
- }
- stat.close();
- c.close();
- System.out.println("Tabela wyczyszczona");
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- //////
- public void run(){
- boolean uruchomiony = true;
- try{
- while(uruchomiony){
- String message = (String) input.readObject();
- System.out.println("Klient id="+id+" wiadomosc: " + message + " Data: "+new Date().toString());
- if(message.equals("koniec")){
- break;
- }else if(message.equals("wyslij na serwer")){
- //output.writeObject("Nazwa uzytkownika");
- String nazwaUzytkownika = (String) input.readObject();
- //output.writeObject("Liczba punktow");
- int punkty = (int) input.readObject();
- serwer.dodajStatystyke(nazwaUzytkownika, punkty);
- }else if(message.equals("pobierz statystyki")){
- output.writeObject(serwer.pobierzStatystyki());
- }
- }
- }catch(Exception ex){
- }finally{
- System.out.println("Rozlaczono z klientem id=" +id );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement