package tests;

import java.sql.*;
import java.util.Properties;

/*
 * @(#)PostgreSQL_Timestamp_Test.java	1.0 Created on Apr 21, 2005 2:19:54 PM
 *
 * Copyright 2002-2003 Mainsoft Corporation. All Rights Reserved.
 *
 * This software is the proprietary information of Mainsoft Corporation.
 * Use is subject to license terms.
 *
 */

public class PostgreSQL_Timestamp_Test
{
    public static void main(String[] args)
    {
        Connection connection = connect("xp050", "", "GHTDB", "postgres", "postgres", false);
        Timestamp ts = Timestamp.valueOf("2004-08-09 20:30:15.50");

        try
        {
            PreparedStatement pstmt = connection.prepareStatement("delete from borisk_timestamp");
            pstmt.execute();

            pstmt = connection.prepareStatement("insert into borisk_timestamp values(?)");
            pstmt.setTimestamp(1, ts);

            pstmt.execute();

            pstmt = connection.prepareStatement("select * from borisk_timestamp");
            pstmt.execute();

            ResultSet rs = pstmt.getResultSet();
            rs.next();
            Timestamp ts1 = rs.getTimestamp(1);
            System.out.println(ts);
            System.out.println(ts1);

            pstmt = connection.prepareStatement("delete from borisk_timestamp");
            pstmt.execute();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    private static Connection connect(String host, String port, String catalog, String user, String pass, boolean trace)
    {
        String conStr = buildUrl(host, port, catalog);

        try
        {
            activateJDBCDriver(getDriverName());
            Properties info = new Properties();

            info.put("user", user);
            info.put("password", pass);

            if (trace)
            {
                System.out.println("Getting connection to host " + host + "  port " + port + " catalog " + catalog);
            }
            DriverManager.setLoginTimeout(15);
            Connection con = DriverManager.getConnection(conStr, info);
            if (trace)
            {
                System.out.println("Connected to host " + host);
            }
            return con;
        }
        catch (SQLException e)
        {
            if (trace)
            {
                System.out.println("Error connecting to host " + host + " : " + e.getMessage());
            }
        }

        return null;
    }

    private static void activateJDBCDriver(String _driver)
    {
        try
        {
            Class.forName(_driver).newInstance();
        }
        catch (InstantiationException e)
        {
            e.printStackTrace();
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    private static String buildUrl(String host, String port, String catalog)
    {
        return "jdbc:postgresql://" + host + "/" + catalog;
    }

    private static String getDriverName()
    {
        return "org.postgresql.Driver";
    }
}
