Guest User

Untitled

a guest
Mar 15th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. /*
  2. * # How to use this script
  3. * 0. Place Redshift JDBC driver onto the local directory
  4. * $ ls -al ./RedshiftJDBC42-1.2.10.1009.jar
  5. * 1. Compile
  6. * $ javac connection/Connect.java
  7. * 2. Execute
  8. * $ java -classpath .:RedshiftJDBC42-1.2.10.1009.jar connection.Connect
  9. */
  10.  
  11. package connection;
  12.  
  13. import java.sql.*;
  14. import java.util.Properties;
  15.  
  16. public class Connect {
  17. // Redshift driver: "jdbc:redshift://x.y.us-west-2.redshift.amazonaws.com:5439/dev";
  18. // or "jdbc:postgresql://x.y.us-west-2.redshift.amazonaws.com:5439/dev";
  19. static final String dbURL = "***jdbc cluster connection string ****";
  20. static final String MasterUsername = "***master user name***";
  21. static final String MasterUserPassword = "***master user password***";
  22.  
  23. public static void main(String[] args) {
  24. Connection conn = null;
  25. Statement stmt = null;
  26. try{
  27. //Dynamically load driver at runtime.
  28. //Redshift JDBC 4.1 driver: com.amazon.redshift.jdbc41.Driver
  29. //Redshift JDBC 4 driver: com.amazon.redshift.jdbc4.Driver
  30. Class.forName("com.amazon.redshift.jdbc.Driver");
  31.  
  32. //Open a connection and define properties.
  33. System.out.println("Connecting to database...");
  34. Properties props = new Properties();
  35.  
  36. //Uncomment the following line if using a keystore.
  37. //props.setProperty("ssl", "true");
  38. props.setProperty("user", MasterUsername);
  39. props.setProperty("password", MasterUserPassword);
  40. conn = DriverManager.getConnection(dbURL, props);
  41.  
  42. //Try a simple query.
  43. System.out.println("Listing system tables...");
  44. stmt = conn.createStatement();
  45. String sql;
  46. sql = "select * from information_schema.tables;";
  47. ResultSet rs = stmt.executeQuery(sql);
  48.  
  49. //Get the data from the result set.
  50. while(rs.next()){
  51. //Retrieve two columns.
  52. String catalog = rs.getString("table_catalog");
  53. String name = rs.getString("table_name");
  54.  
  55. //Display values.
  56. System.out.print("Catalog: " + catalog);
  57. System.out.println(", Name: " + name);
  58. }
  59. rs.close();
  60. stmt.close();
  61. conn.close();
  62. }catch(Exception ex){
  63. //For convenience, handle all errors here.
  64. ex.printStackTrace();
  65. }finally{
  66. //Finally block to close resources.
  67. try{
  68. if(stmt!=null)
  69. stmt.close();
  70. }catch(Exception ex){
  71. }// nothing we can do
  72. try{
  73. if(conn!=null)
  74. conn.close();
  75. }catch(Exception ex){
  76. ex.printStackTrace();
  77. }
  78. }
  79. System.out.println("Finished connectivity test.");
  80. }
  81. }
Add Comment
Please, Sign In to add comment