Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. /*
  4. * Connection execute return resultSet <--- griglia di dati Classe Connection
  5. * Factory() Class Dao
  6. */
  7. Main m = new Main();
  8. try {
  9. m.execute();
  10. } catch (Exception e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14.  
  15. }
  16.  
  17. private void execute() throws Exception {
  18.  
  19. // Istanza Connessione
  20. Connection con = getConnection();
  21. Statement statment = con.createStatement();
  22. ResultSet rs = statment.executeQuery("select city from city");
  23. ResultSetMetaData rsmd = rs.getMetaData();
  24. int columnsNumber = rsmd.getColumnCount();
  25. while (rs.next()) {
  26. for (int i = 1; i <= columnsNumber; i++) {
  27. if (i > 1)
  28. System.out.print(", ");
  29. String columnValue = rs.getString(i);
  30. System.out.print(columnValue + " " + rsmd.getColumnName(i));
  31. }
  32. System.out.println("");
  33. }
  34. con.close();
  35. }
  36.  
  37. private Connection getConnection() throws Exception {
  38. Class.forName("com.mysql.jdbc.Driver");
  39. Connection conn = null;
  40. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "alternanza");
  41. return conn;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement