Guest User

Untitled

a guest
Mar 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1.  
  2. /**
  3. * To test connection to the PostgreSQL DB using TellyMaker DB connection params.
  4. */
  5.  
  6.  
  7. // Imports
  8.  
  9. import java.lang.*;
  10. import java.io.*;
  11. import java.sql.*;
  12.  
  13. public class TestDBConnect
  14. {
  15.  
  16. public static void main(String[] args)
  17. {
  18. // variable to store the JDBC connection.
  19. Connection connection = null;
  20.  
  21. try
  22. {
  23. debug("trying to connect to pg db server ...");
  24. establishConnection(connection);
  25. debug("after establishConnection.");
  26. debug("connection value is: " + connection);
  27. connection.close();
  28. }
  29. catch (Exception e)
  30. {
  31. debug("Caught Exception, exiting main");
  32. debug(e.getMessage());
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. // Get the JDBC parameters to connect to the database.
  38.  
  39. public static String[] getJDBCParameters()
  40. {
  41. debug("Entered TestDBConnect.getJDBCParameters()");
  42. // Parameters are hard coded for now.
  43.  
  44. // TODO: Change hardcoding; get parameters from a properties file,
  45. // or other way.
  46. // (Once this code is converted to be called by a Web Service,
  47. // the services_configuration file should be the place to
  48. // put this info.)
  49.  
  50. // Using 10 for the array length to be safe, actual number of
  51. // items in it is less (3 as of now).
  52. String jdbcParameters[] = new String[10];
  53. int i;
  54.  
  55. // Initialize the array to nulls.
  56. for (i = 0; i < 10; i++)
  57. jdbcParameters[i] = null;
  58.  
  59. // TODO: change the 3 parameters below to correct ones.
  60. // JDBC URL to database
  61. jdbcParameters[0] = "jdbc:postgresql://10.10.10.215:5432/tellymaker";
  62. // username
  63. jdbcParameters[1] = "maker1postgres";
  64. // password
  65. jdbcParameters[2] = "postgrestmp47";
  66. debug("Exiting TestDBConnect.getJDBCParameters()");
  67. return jdbcParameters;
  68. }
  69.  
  70. // Establish a JDBC Connection to the database.
  71.  
  72. public static void establishConnection(Connection connection)
  73. {
  74. String[] jdbcParameters = getJDBCParameters();
  75. try
  76. {
  77. // Load the PostgreSQL JDBC driver class
  78. Class.forName("org.postgresql.Driver");
  79.  
  80. // Get a connection to the DB.
  81. connection = DriverManager.getConnection(
  82. jdbcParameters[0], jdbcParameters[1], jdbcParameters[2]);
  83. }
  84. catch (ClassNotFoundException cnfe)
  85. {
  86. debug("Caught ClassNotFoundException: " +
  87. "Could not load PostgreSQL JDBC driver:");
  88. debug(cnfe.getMessage());
  89. cnfe.printStackTrace();
  90. // TODO: Change the line below to return a failure code,
  91. // from the web service, instead of doing a System.exit().
  92. System.exit(1);
  93. }
  94. catch (SQLException sqle)
  95. {
  96. debug("Caught SQL Exception:");
  97. debug(sqle.getMessage());
  98. sqle.printStackTrace();
  99. // TODO: Change the line below to return a failure code,
  100. // from the web service, instead of doing a System.exit().
  101. System.exit(1);
  102. }
  103. catch (Exception e)
  104. {
  105. debug("Caught Exception:");
  106. debug(e.getMessage());
  107. e.printStackTrace();
  108. // TODO: Change the line below to return a failure code,
  109. // from the web service, instead of doing a System.exit().
  110. System.exit(1);
  111. }
  112. }
  113.  
  114. // Debug method.
  115.  
  116. private static void debug(String message)
  117. {
  118. System.err.println(message);
  119. }
  120.  
  121. }
  122.  
  123. /**********************************************************
  124.  
  125. **********************************************************/
Add Comment
Please, Sign In to add comment