import java.sql.*;

public class pgtest {
    private static final String url = "jdbc:postgresql://localhost:5432:thomas";
    private static final String username = "thomas";
    private static final String password = "";
    private static final String tablename = "time_interval_test";

    public static void main(String[] args) {
	try {
	    Class.forName("org.postgresql.Driver");
	    Connection con = DriverManager.getConnection(url, username, password);
	    Statement stmt = con.createStatement();

	    ////////////////////////////////////
	    ///// First, create test table /////
	    ////////////////////////////////////

	    try {
		stmt.executeUpdate("create table " + tablename + " ("
			+ " col1 varchar(10) primary key,"
			+ " col2 time(3),"
			+ " col3 time(3) with time zone,"
			+ " col4 timestamp(3),"
			+ " col5 timestamp(3) with time zone,"
			+ " col6 interval(3) )");
		stmt.executeUpdate("insert into " + tablename + " values ("
			+ " 'foo',"
			+ " '9:45:10.123',"
			+ " '9:45:10.123 CET',"
			+ " '2005-12-03 9:45:10.123',"
			+ " '2005-12-03 9:45:10.123 CET',"
			+ " '1 year 2 months 3 days 4 hours 5 minutes 6.123 seconds' )");
	    } catch (SQLException e) {}

	    /////////////////////////////////////////////////////////////
	    ///// Retrieve column information from DatabaseMetaData /////
	    /////////////////////////////////////////////////////////////

	    DatabaseMetaData dbmd = con.getMetaData();
	    ResultSet rs = dbmd.getColumns(null, null, tablename, null);
	    System.out.println();
	    System.out.println("Column information for " + tablename
			+ " from DatabaseMetaData:");
	    System.out.println();
	    while (rs.next()) {
		String name = rs.getString("COLUMN_NAME");
		String type = rs.getString("TYPE_NAME");
		int size = rs.getInt("COLUMN_SIZE");
		boolean sizeIsNull = rs.wasNull();
		int scale = rs.getInt("DECIMAL_DIGITS");
		boolean scaleIsNull = rs.wasNull();
		boolean notnull = "no".equalsIgnoreCase(
			rs.getString("IS_NULLABLE"));
		System.out.println(name + ": " + type + "("
			+ (sizeIsNull ? "null" : Integer.toString(size)) + ", "
			+ (scaleIsNull ? "null" : Integer.toString(scale)) + ")"
			+ (notnull ? " not null" : ""));
	    }
	    rs.close();

	    //////////////////////////////////////////////////////////////
	    ///// Retrieve column information from ResultSetMetaData /////
	    //////////////////////////////////////////////////////////////

	    rs = stmt.executeQuery("select * from " + tablename);
	    ResultSetMetaData rsmd = rs.getMetaData();
	    System.out.println();
	    System.out.println("Column information for " + tablename
			+ " from ResultSetMetaData:");
	    System.out.println();
	    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
		String name = rsmd.getColumnName(i);
		String type = rsmd.getColumnTypeName(i);
		int size = rsmd.getPrecision(i);
		int scale = rsmd.getScale(i);
		boolean notnull =
		    rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls;
		System.out.println(name + ": " + type + "(" + size + ", "
			+ scale + ")" + (notnull ? " not null" : ""));
	    }
	    System.out.println();
	    rs.close();
	    stmt.close();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
