Advertisement
Guest User

Untitled

a guest
May 16th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.sql.Timestamp;
  7. import java.sql.Time;
  8. import java.util.Date;
  9.  
  10. public class readUser {
  11. /**
  12. * The JDBC driver to be used
  13. */
  14. private static final String DRIVER = "org.postgresql.Driver";
  15. /**
  16. * The URL of the database to be accessed
  17. */
  18. private static final String DATABASE = "jdbc:postgresql://localhost:5433/db1516";
  19. /**
  20. * The username for accessing the database
  21. */
  22. private static final String USER = "db1516";
  23. /**
  24. * The password for accessing the database
  25. */
  26. private static final String PASSWORD = "quaiyiva";
  27. /**
  28. * The SQL statement to be executed
  29. */
  30. private static final String SQL = "SELECT E.*, TY.Nome_tipo, TH.Nome_tema FROM gallina.evento AS E INNER JOIN gallina.Tipo_evento AS TY ON E.id_evento=TY.id_evento INNER JOIN gallina.Tema_evento AS TH ON E.id_evento=TH.id_evento WHERE E.id_evento = '3bc566f8-ea1f-4f41-b822-72b5465d0c8e' UNION SELECT E.*, TY.Nome_tipo, TH.Nome_tema FROM gallina.evento AS E LEFT JOIN gallina.Tipo_evento AS TY ON E.id_evento=TY.id_evento LEFT JOIN gallina.Tema_evento AS TH ON E.id_evento=TH.id_evento WHERE E.id_sopra_evento='3bc566f8-ea1f-4f41-b822-72b5465d0c8e';";
  31. /**
  32. * List the special need of the participants
  33. *
  34. * @param args
  35. * command-line arguments (not used).
  36. */
  37. public static void main(String[] args) {
  38. // the connection to the DBMS
  39. Connection con = null;
  40. // the statement to be executed
  41. Statement stmt = null;
  42. // the results of the statement execution
  43. ResultSet rs = null;
  44. // start time of a statement
  45. long start;
  46. // end time of a statement
  47. long end;
  48. // "data structures" for the data to be read from the database
  49. // the data about the participant
  50.  
  51. String id_evento = null;
  52. String id_sopra_evento = null;
  53. String titolo = null, descrizione = null, via = null, cap = null, civico = null, nome = null, email = null, telefono = null;
  54. Date data_inizio = null, data_fine = null;
  55. Timestamp data_inizio_prenotazioni = null, data_fine_prenotazioni = null;
  56. Time orario_apertura = null, orario_chiusura = null;
  57. int numero_posti_disponibili = 0;
  58.  
  59. try {
  60. // register the JDBC driver
  61. Class.forName(DRIVER);
  62. System.out.printf("Driver %s successfully registered.%n", DRIVER);
  63. } catch (ClassNotFoundException e) {
  64. System.out.printf("Driver %s not found: %s.%n", DRIVER, e.getMessage());
  65. // terminate with a generic error code
  66. System.exit(-1);
  67. }
  68. try {
  69. // connect to the database
  70. start = System.currentTimeMillis();
  71. con = DriverManager.getConnection(DATABASE, USER, PASSWORD);
  72. end = System.currentTimeMillis();
  73. System.out.printf("Connection to database %s successfully established in%,d milliseconds.%n",DATABASE, end-start);
  74. // create the statement to execute the query
  75. start = System.currentTimeMillis();
  76. stmt = con.createStatement();
  77. end = System.currentTimeMillis();
  78. System.out.printf("Statement successfully created in %,d milliseconds.%n",end-start);
  79. // execute the query
  80. start = System.currentTimeMillis();
  81. rs = stmt.executeQuery(SQL);
  82. end = System.currentTimeMillis();
  83. System.out.printf("Query %s successfully executed %,d milliseconds.%n",SQL, end - start);
  84. System.out.printf("Query results:%n");
  85. // cycle on the query results and print them
  86. while (rs.next()) {
  87. // Legge l'id dell'evento
  88. id_evento = rs.getString("id_evento");
  89. id_sopra_evento = rs.getString("id_sopra_evento");
  90. descrizione = rs.getString("descrizione");
  91. via = rs.getString("via");
  92. cap = rs.getString("cap");
  93. civico = rs.getString("civico");
  94. nome = rs.getString("nome");
  95. email = rs.getString("email");
  96. telefono = rs.getString("telefono");
  97. data_inizio = rs.getDate("data_inizio");
  98. data_fine = rs.getDate("data_fine");
  99. data_inizio_prenotazioni = rs.getTimestamp("data_inizio_prenotazioni");
  100. data_fine_prenotazioni = rs.getTimestamp("data_fine_prenotazioni");
  101. orario_apertura = rs.getTime("orario_apertura");
  102. orario_chiusura = rs.getTime("orario_chiusura");
  103. numero_posti_disponibili = rs.getInt("numero_posti_disponibili");
  104.  
  105. // Legge il titolo dell'evento
  106. titolo = rs.getString("titolo");
  107. System.out.printf("- %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, "+ data_inizio.toString() + ", " + data_fine.toString() + ", " + data_inizio_prenotazioni.toString() + ", " + data_fine_prenotazioni.toString() + ", " + orario_apertura.toString() + ", " + orario_chiusura.toString() + " %n", id_evento, id_sopra_evento, descrizione, via, cap, civico, nome, email, telefono, numero_posti_disponibili);
  108. }
  109. } catch (SQLException e) {
  110. System.out.printf("Database access error:%n");
  111. // cycle in the exception chain
  112. while (e != null) {
  113. System.out.printf("- Message: %s%n", e.getMessage());
  114. System.out.printf("- SQL status code: %s%n", e.getSQLState());
  115. System.out.printf("- SQL error code: %s%n", e.getErrorCode());
  116. System.out.printf("%n");
  117. e = e.getNextException();
  118. }
  119. } finally {
  120. try {
  121. // close the used resources
  122. if (rs != null) {
  123. start = System.currentTimeMillis();
  124. rs.close();
  125. end = System.currentTimeMillis();
  126. System.out.printf("Result set successfully closed in %,d milliseconds.%n",end-start);
  127. }
  128. if (stmt != null) {
  129. start = System.currentTimeMillis();
  130. stmt.close();
  131. end = System.currentTimeMillis();
  132. System.out
  133. .printf("Statement successfully closed in %,d milliseconds.%n",end-start);
  134. }
  135. if (con != null) {
  136. start = System.currentTimeMillis();
  137. con.close();
  138. end = System.currentTimeMillis();
  139. System.out
  140. .printf("Connection successfully closed in %,d milliseconds.%n",end-start);
  141. }
  142. System.out.printf("Resources successfully released.%n");
  143. } catch (SQLException e) {
  144. System.out.printf("Error while releasing resources:%n");
  145. // cycle in the exception chain
  146. while (e != null) {
  147. System.out.printf("- Message: %s%n", e.getMessage());
  148. System.out.printf("- SQL status code: %s%n",e.getSQLState());
  149. System.out.printf("- SQL error code: %s%n",e.getErrorCode());
  150. System.out.printf("%n");
  151. e = e.getNextException();
  152. }
  153. } finally {
  154. // release resources to the garbage collector
  155. rs = null;
  156. stmt = null;
  157. con = null;
  158. System.out.printf("Resources released to the garbage collector.%n");
  159. }
  160. }
  161. System.out.printf("Program end.%n");
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement