Advertisement
Guest User

DbManager

a guest
Mar 26th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.*;
  4.  
  5. public final class DbManager {
  6.  
  7. public DbManager() {
  8. dbUser = System.getenv("db_user");
  9. dbPass = System.getenv("db_pass");
  10. dbUrl = System.getenv("db_url");
  11. }
  12.  
  13.  
  14. public void inserisciRegistrazione(UtenteBean newUtente) {
  15.  
  16. try {
  17. // Register JDBC driver
  18. Class.forName("com.mysql.cj.jdbc.Driver");
  19.  
  20. // Open a connection with a try-with-resources: non c'e' bisogno di chiamare la funzione close su conn, verrà chiusa alla fine del body
  21. try (Connection conn = DriverManager.getConnection(this.dbUrl, this.dbUser, this.dbPass)) {
  22. // Execute SQL query
  23. try (Statement stmt = conn.createStatement()) {
  24. String sql;
  25. sql = "INSERT INTO USER(email,username,nome,cognome,nazione,eta,password)VALUES(" + newUtente.getEmail() + ',' + newUtente.getUsername() + ',' + newUtente.getNome() + ',' + newUtente.getCognome() + ',' + newUtente.getNazione() + ',' + newUtente.getEta() + ',' + newUtente.getPassword() + ")";
  26.  
  27. }
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. } catch (ClassNotFoundException e) {
  32. // Handle errors for JDBC
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public UtenteBean userLogin(UtenteBean utente) {
  38.  
  39. UtenteBean user = new UtenteBean();
  40.  
  41. try {
  42. // Register JDBC driver
  43. Class.forName("com.mysql.cj.jdbc.Driver");
  44.  
  45. // Open a connection with a try-with-resources: non c'e' bisogno di chiamare la funzione close su conn, verrà chiusa alla fine del body
  46. try (Connection conn = DriverManager.getConnection(this.dbUrl, this.dbUser, this.dbPass)) {
  47. // Execute SQL query
  48. try (Statement stmt = conn.createStatement()) {
  49. String sql;
  50. sql = "SELECT * FROM user WHERE username = '" + utente.getUsername() + "' AND password = '" + utente.getPassword() + "'";
  51. stmt.executeQuery(sql);
  52. ResultSet rs = stmt.getResultSet();
  53.  
  54. if (rs.next()) {
  55. utente.setUsername(rs.getString("username"));
  56. utente.setPassword(rs.getString("password"));
  57. utente.setEmail(rs.getString("email"));
  58. utente.setNome(rs.getString("nome"));
  59. utente.setCognome(rs.getString("cognome"));
  60. utente.setEta(rs.getString("nazione"));
  61. utente.setNazione(rs.getString("eta"));
  62. }
  63.  
  64. return user;
  65.  
  66. }
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. } catch (ClassNotFoundException e) {
  71. // Handle errors for JDBC
  72. e.printStackTrace();
  73. }
  74.  
  75. return utente;
  76. }
  77.  
  78.  
  79.  
  80. private final String dbUser;
  81. private final String dbPass;
  82. private final String dbUrl;
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement