Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * To test connection to the PostgreSQL DB using TellyMaker DB connection params.
- */
- // Imports
- import java.lang.*;
- import java.io.*;
- import java.sql.*;
- public class TestDBConnect
- {
- public static void main(String[] args)
- {
- // variable to store the JDBC connection.
- Connection connection = null;
- try
- {
- debug("trying to connect to pg db server ...");
- establishConnection(connection);
- debug("after establishConnection.");
- debug("connection value is: " + connection);
- connection.close();
- }
- catch (Exception e)
- {
- debug("Caught Exception, exiting main");
- debug(e.getMessage());
- e.printStackTrace();
- }
- }
- // Get the JDBC parameters to connect to the database.
- public static String[] getJDBCParameters()
- {
- debug("Entered TestDBConnect.getJDBCParameters()");
- // Parameters are hard coded for now.
- // TODO: Change hardcoding; get parameters from a properties file,
- // or other way.
- // (Once this code is converted to be called by a Web Service,
- // the services_configuration file should be the place to
- // put this info.)
- // Using 10 for the array length to be safe, actual number of
- // items in it is less (3 as of now).
- String jdbcParameters[] = new String[10];
- int i;
- // Initialize the array to nulls.
- for (i = 0; i < 10; i++)
- jdbcParameters[i] = null;
- // TODO: change the 3 parameters below to correct ones.
- // JDBC URL to database
- jdbcParameters[0] = "jdbc:postgresql://10.10.10.215:5432/tellymaker";
- // username
- jdbcParameters[1] = "maker1postgres";
- // password
- jdbcParameters[2] = "postgrestmp47";
- debug("Exiting TestDBConnect.getJDBCParameters()");
- return jdbcParameters;
- }
- // Establish a JDBC Connection to the database.
- public static void establishConnection(Connection connection)
- {
- String[] jdbcParameters = getJDBCParameters();
- try
- {
- // Load the PostgreSQL JDBC driver class
- Class.forName("org.postgresql.Driver");
- // Get a connection to the DB.
- connection = DriverManager.getConnection(
- jdbcParameters[0], jdbcParameters[1], jdbcParameters[2]);
- }
- catch (ClassNotFoundException cnfe)
- {
- debug("Caught ClassNotFoundException: " +
- "Could not load PostgreSQL JDBC driver:");
- debug(cnfe.getMessage());
- cnfe.printStackTrace();
- // TODO: Change the line below to return a failure code,
- // from the web service, instead of doing a System.exit().
- System.exit(1);
- }
- catch (SQLException sqle)
- {
- debug("Caught SQL Exception:");
- debug(sqle.getMessage());
- sqle.printStackTrace();
- // TODO: Change the line below to return a failure code,
- // from the web service, instead of doing a System.exit().
- System.exit(1);
- }
- catch (Exception e)
- {
- debug("Caught Exception:");
- debug(e.getMessage());
- e.printStackTrace();
- // TODO: Change the line below to return a failure code,
- // from the web service, instead of doing a System.exit().
- System.exit(1);
- }
- }
- // Debug method.
- private static void debug(String message)
- {
- System.err.println(message);
- }
- }
- /**********************************************************
- **********************************************************/
Add Comment
Please, Sign In to add comment