import java.sql.*;

class tt {

    public static final String connectionString = "jdbc:oracle:thin:@eir:1521:eir";
    public static final String username = "qcms";
    public static final String password = "qcms";

    public static void isolateTransactions(Connection conn) throws SQLException {
	PreparedStatement st = conn.prepareStatement("ALTER SESSION SET ISOLATION_LEVEL = SERIALIZABLE");
	st.executeUpdate();
	st.close();
    }

    public static void main(String[] args) {
	String spec = args[0];
        try {
	    System.out.println("Installing JDBC driver");
	    Class.forName("oracle.jdbc.driver.OracleDriver");

            System.out.println("Logging in to " + connectionString + " as " + username + "/" + password);
	    Connection conn = DriverManager.getConnection(connectionString, username, password);
	    isolateTransactions(conn);
	    conn.setAutoCommit(false);

            String sql = "SELECT ?";
            for (int i = 1; i < spec.length(); i++)
		sql += ", ?";
            sql += " FROM DUAL";
	    System.out.println("Defining query: " + sql);
	    PreparedStatement st = conn.prepareStatement(sql);
            for (int i = 0; i < spec.length(); i++) {
		if (spec.charAt(i) == 'n') {
                    System.out.println("  with " + (1000+i+1));
		    st.setInt(i+1, 1000+i+1);
		} else {
                    System.out.println("  with string " + (i+1));
		    st.setString(i+1, "string " + (i+1));
                }
	    }

	    System.out.println("Executing query");
	    ResultSet r = st.executeQuery();
	    while (r.next()) {
		System.out.println("Returning " + r.getString(1));
                for (int i = 1; i < spec.length(); i++)
		    System.out.println("      and " + r.getString(i+1));
	    }
	    System.out.println("Closing query");
	    st.close();
	    System.out.println("Closing JDBC connection");
	    conn.close();
	    System.out.println("All done");

        } catch (SQLException e) {
            System.out.println("SQLException caught: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException caught: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
}


