Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.net.URLClassLoader;
  4. import java.sql.*;
  5. import java.util.Properties;
  6.  
  7. public class PrintPg {
  8. static String url = "jdbc:postgresql://localhost:5432/postgres";
  9. static String pgJarPath = "file:/path/to/postgresql-9.4.1208.jar";
  10.  
  11. public static void main(String[] args) throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, MalformedURLException {
  12.  
  13. URLClassLoader child = new URLClassLoader(new URL[] { new URL(pgJarPath) }, PrintPg.class.getClassLoader());
  14. Class<?> driverClass = Class.forName("org.postgresql.Driver", true, child);
  15. Driver driver = (Driver) driverClass.newInstance();
  16.  
  17. Properties info = new Properties();
  18. info.setProperty("user", "postgres");
  19. info.setProperty("password", "postgres");
  20. Connection c = driver.connect(url, info);
  21.  
  22. c.setAutoCommit(true);
  23. PreparedStatement stmt = c.prepareStatement("SELECT * FROM information_schema.columns");
  24. ResultSet rs = stmt.executeQuery();
  25.  
  26. while (rs.next()) {
  27. System.out.println(""
  28. + rs.getString("table_catalog") + "\t"
  29. + rs.getString("table_schema") + "\t"
  30. + rs.getString("table_name") + "\t"
  31. + rs.getString("column_name") + "\t"
  32. + rs.getLong("ordinal_position")
  33. );
  34. }
  35.  
  36. rs.close();
  37. c.close();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement