Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package com.gsi.springAlumno;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class MySQLDBConnection {
  10.  
  11. /**
  12. * Registra dinámicamente el driver y devuelve una conexión con la BD
  13. * @return una conexión con la BD
  14. */
  15. public static Connection getConnection() throws SQLException, ClassNotFoundException {
  16.  
  17. // Cargamos el driver
  18. Class.forName("com.mysql.jdbc.Driver");
  19. // Realizamos la conexión
  20. Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/contabilidad","root","");
  21.  
  22. // We want to control transactions manually. Autocommit is on by default in JDBC.
  23. // con.setAutoCommit(false);
  24.  
  25. return con;
  26. }
  27.  
  28. /**
  29. * Cierra la conexión con la BD
  30. * @param conexión a cerrar
  31. */
  32. public static void closeConnection(Connection con) throws SQLException {
  33. con.close();
  34. }
  35.  
  36. public static void getMetadata(Connection con) {
  37. try {
  38. DatabaseMetaData dbmd = con.getMetaData();
  39. System.out.println("Driver Name: "+dbmd.getDriverName());
  40. System.out.println("Driver Version: "+dbmd.getDriverVersion());
  41.  
  42. ResultSet rs = dbmd.getTables(null, null, null, null);
  43.  
  44. while (rs.next()) {
  45. String tab = rs.getString("TABLE_NAME");
  46. System.out.println("Tabla: "+tab);
  47.  
  48. ResultSet cols = dbmd.getColumns(null,null,tab,null);
  49.  
  50. while (cols.next()) {
  51. String col = cols.getString("COLUMN_NAME");
  52. String tipo = cols.getString("TYPE_NAME");
  53. System.out.print(" Columna: "+col);
  54. System.out.println(" - Tipo: "+tipo);
  55. }
  56. }
  57. } catch (SQLException e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement