/**
 * A simple wrapper for establishing connection to a PostgreSQL database
 */

import java.sql.*;

public class Postgres {
    /**
     * Establish a connection to a PostgreSQL database server.
     * Return null if the connection is unsuccessful.
     */
    public static Connection getConnection(String host, String dbName,
	    String user, String password) {
	// load the PostgreSQL JDBC driver
	try {
	    Class.forName("org.postgresql.Driver");
	} catch (ClassNotFoundException ex) {
	    System.err.println(ex);
	}

	// connecting to the database
	Connection con = null;
	try {
	    // composing the connection URL, assuming the database
	    // server is listening on the default port (5432)
	    String url = "jdbc:postgresql://" + host + "/" + dbName;
	    con = DriverManager.getConnection(url, user, password);

	    // verify the connection is successful by displaying
	    // database software name and version
	    DatabaseMetaData metaData = con.getMetaData();
	    System.out.println("Connected to " +
		    metaData.getDatabaseProductName() + " " +
		    metaData.getDatabaseProductVersion());
	} catch (SQLException ex) {
	    System.err.println(ex);
	}

	return con;
    }

    public static void main(String[] args) {
	// TODO: fill in your database settings and test the connection
	//getConnection("localhost", "rosetta", "cs329e", "");
	getConnection("dna.csres.utexas.edu:5432", "mr", "weijia", "iamweijia");
    }
}

