import java.sql.*;
import java.util.UUID;

/*

 create table t_jtest (
	ikey CHAR(36) primary key,
	ivalue integer not null
);
 
 */


public class DbIject {

	/**
	 * @param args
	 */
	public static void main(String[] args) {        
        
        try {
        	
        	Class.forName("org.postgresql.Driver");
        	
        	Connection newConnection = null;
        	
        
            newConnection = DriverManager.getConnection("jdbc:postgresql://localhost/jTest?prepareThreshold=3",
            		"postgres", "");
            newConnection.setAutoCommit(false);
            
            
           String firstId = null;
            
            for(int i = 0; ; i++) {
            	
            	PreparedStatement ps = newConnection.prepareStatement("INSERT INTO t_jtest VALUES(?,?)");
            	
            	String id = UUID.randomUUID().toString();
            	long now = System.currentTimeMillis();
            	
            	
            	
            	ps.setString(1, id );
            	ps.setInt(2, i);
            	
            	ps.executeUpdate();
            	            	
            	ps.close();
            	
            	if(i == 0 ) {
            		firstId = id;
            	}else{
            		
                	ps = newConnection.prepareStatement("UPDATE t_jtest SET ivalue=? WHERE ikey=?");            	
                	
                	ps.setString(2, firstId );
                	ps.setInt(1, i);
                	
                	ps.executeUpdate();
                	ps.close(); 
            	}
            	
            	
            	newConnection.commit();
            	System.out.println(i + " " + (System.currentTimeMillis() - now) );
            	
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }

	}

}
