Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public void registarUtilizador(String username, String password) throws SQLException {
  2.  
  3. Connection conn = getConnection();
  4.  
  5. Statement stat = conn.createStatement();
  6.  
  7. int id_utilizador = randomGenerator.nextInt(1000);
  8.  
  9. try {
  10. stat.execute("INSERT into Utilizadores (id_utilizador,username,password) VALUES ('"+id_utilizador+"','"+username+"','"+password+"')");
  11. System.out.println("Utilizador registado");
  12. conn.commit();
  13.  
  14. } catch (SQLException e) {
  15. // log the warning...
  16. e.printStackTrace();
  17. }
  18.  
  19. }
  20.  
  21. public void login(String username, String password) throws SQLException {
  22.  
  23. int flag=0;
  24.  
  25. Connection conn = getConnection();
  26.  
  27. String query = "SELECT Utilizadores.username, Utilizadores.password FROM Utilizadores WHERE Utilizadores.username = ('"+username+"') AND Utilizadores.password = ('"+password+"')";
  28.  
  29. Statement stat = conn.createStatement();
  30.  
  31. try {
  32. ResultSet rs = stat.executeQuery(query);
  33. while(rs.next()) {
  34. System.out.println("Utilizador autenticado");
  35. flag = 1;
  36. }
  37.  
  38. if (flag==0)
  39. System.out.println("Utilizador não autenticado");
  40.  
  41.  
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement