Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. try {
  2.             // Mi connetto al database
  3.             Connection conn = DriverManager.getConnection(connectionString, "carlocabras", "0");
  4.            
  5.             // Preparo la query con cui ricerco tutti i clienti e filtro per usr e psw
  6.             String queryRicerca = "select * from cliente where " + "username = ? and password = ?";
  7.             PreparedStatement stmt = conn.prepareStatement(queryRicerca);
  8.            
  9.             // Setto le due stringhe ? dello statement con i valori passati al metodo
  10.             stmt.setString(1, usr);
  11.             stmt.setString(2, psw);
  12.            
  13.             // Mando in esecuzione la query
  14.             ResultSet res = stmt.executeQuery();
  15.            
  16.             //-----------------------------------------------------------------------------------------------
  17.             // PROVA INSERIMENTO--------------------------------------------------------------------------
  18.             stmt = conn.prepareStatement("INSERT INTO cliente (id, nome, cognome, codice_fiscale, data_nascita, saldo, username, password) " +
  19.                                          "VALUES (default, 'Prova', 'prova', 'CBRCRL93S26V234A','1993-11-26',1000,'provaprova','0')");
  20.             ResultSet rs = stmt.executeQuery(); //NOTARE CHE rs E NON res
  21.            
  22.             // In set ci deve essere un solo elemento (cliente) associato a quell'username e password. Mi assicuro che ci sia
  23.             if(res.next()) {
  24.                 // Creo un nuovo Cliente, da valorizzare e poi restituire
  25.                 Cliente c = new Cliente();
  26.                 c.setId(res.getInt("id"));
  27.                 c.setNome(res.getString("nome"));
  28.                 c.setCognome(res.getString("cognome"));
  29.                 c.setUsr(res.getString("username"));
  30.                 c.setPsw(res.getString("password"));
  31.                 c.setDataNascita(res.getString("data_nascita"));
  32.                 c.setSaldo(new Saldo(res.getDouble("saldo")));
  33.                 c.setCodiceFiscale(res.getString("codice_fiscale"));
  34.                
  35.                 stmt.close();
  36.                 conn.close();
  37.                
  38.                 return c;
  39.              
  40.             }
  41.            
  42.             stmt.close();
  43.             conn.close();
  44.            
  45.            
  46.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement