Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public static void main(String[] args) {
  2.  
  3. final String DRIVER = "com.mysql.cj.jdbc.Driver";
  4.  
  5. final String DB_URL_COMPLETA = "jdbc:mysql://localhost:3306/AgenziaImmobiliare?user=root&password=corso&serverTimezone=UTC";
  6. final String DB_URL = "jdbc:mysql://localhost:3306/AgenziaImmobiliare?serverTimezone=UTC";
  7. final String DB_USER = "root";
  8. final String DB_PASSWORD = "corso";
  9.  
  10. // 1. Verificare il name del driver
  11. try {
  12. Class.forName(DRIVER).newInstance();
  13. } catch (Exception e) {
  14. System.out.println("Errore con Class.forName()");
  15. }
  16.  
  17. // 2. Stabilire la connessione al database MySQL
  18. Connection con = null;
  19. try {
  20. con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
  21. // Connection con2 = DriverManager.getConnection(DB_URL_COMPLETA);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. System.out.println("Errore con la connessione al DB");
  25. }
  26.  
  27. // 3. Query
  28. try {
  29. if (con != null) {
  30. Statement statement = con.createStatement();
  31. String query = "SELECT * FROM immobile";
  32. ResultSet res = statement.executeQuery(query);
  33. while (res.next()) {
  34. System.out.print(res.getString("id") + " ");
  35. System.out.println(res.getString(2) + "");
  36. }
  37. }
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement