import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBTest {

  public static void main(String[] args) throws Exception {
    if (args.length != 6) {
      System.err.println("Usage: host port dbname user pass schema");
      System.exit(1);
    }
    
    Long result = null;
    
    loadDriver();
    
    Connection conn = getConnection(args[0], args[1], args[2], args[3], args[4], args[5]);
    
    System.out.println("Fetching statement");
    
    result = getDefault(conn);
    
    System.out.println("Fetched: " + result);
    
    System.out.println("Fetching prepared statement");
    
    result = getPreparedStatement(conn);
    
    System.out.println("Fetched: " + result);
    
    conn.close();
    
  }
  
  public static Long getDefault(Connection conn) throws SQLException {
    Statement statement = conn.createStatement();
    
    System.out.println("First S Fetch");
    ResultSet resultSet = statement.executeQuery("select count(*) from massive_table where int_column = 1");

    resultSet.next();
    
    System.out.println("Returning S Fetch");
    
    Long res = resultSet.getLong(1);
    
    resultSet.close();
    statement.close();
    
    return res;
  }

  public static Long getPreparedStatement(Connection conn) throws SQLException {
    PreparedStatement stat = conn.prepareStatement("select count(*) from massive_table where int_column = ?");
    
    stat.setInt(1, 1);
    
    System.out.println("First PS Fetch");
    ResultSet resultSet = stat.executeQuery();
    
    resultSet.close();
    
    stat.setObject(1, 1);
    
    System.out.println("Second PS Fetch");
    resultSet = stat.executeQuery();
    
    resultSet.next();
    
    System.out.println("Returning PS Fetch");
    
    Long res = resultSet.getLong(1);
    
    resultSet.close();
    stat.close();
    
    return res;
  }
  
  public static void loadDriver() {
    try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException cnfe) {
      System.err.println("Error: Registering driver failed. Database driver not found: " + cnfe.getMessage());
    } catch (Exception e) {
      System.err.println("Database driver error: " + e.getMessage());
    }

    System.out.println("Database driver successfully registered.");
  }

  public static Connection getConnection(String host, String port, String name, String username, String password, String schema) throws Exception {
    String url = "jdbc:postgresql://" + host + ":" + port + "/" + name;
    
    Connection connection = null;
    
    Properties props = new Properties();
    props.setProperty("user", username);
    props.setProperty("password", password);
    //props.setProperty("prepareThreshold", "1");
    //props.setProperty("stringType", "unspecified");
    //props.setProperty("allowEncodingChanges", "false");
    //props.setProperty("protocolVersion", "2");
    //props.setProperty("ssl", "true");
    
    connection = DriverManager.getConnection(url, props);
    
    if (schema.trim().length() > 0) { 
      Statement stat = connection.createStatement();
      
      stat.execute("set search_path = '" + schema + "';");
      
      stat.close();
    }

    return connection;
  }
}