import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

class FRtest
{
	static Connection connection = null;
	static String db_string = "jdbc:jtds:sqlserver://virtual_acao:1433;databaseName=acao_pro;user=acao;password=acao";
	static String getName(int code, boolean close, boolean display) throws Exception
	{
		String sql = "select Cognome,Nome from soci_dati_generale where codice_socio_dati_generale = ? ";
		if (close)
		{	connection = DriverManager.getConnection(db_string);
		}
		PreparedStatement query = connection.prepareStatement(sql);
		query.setInt(1, code);
		ResultSet result = query.executeQuery();
		String name;
		if (result.next()) 
		{	name = code + " " + result.getString(1) + " " + result.getString(2);
		} else 
		{	name = code + " unknown";
		}
		result.close();
		query.close();
		if (close)
		{	connection.close();
			connection = null;
		}
		if (display)
		{ 	System.out.println(name);
		}
		return name;
	}

	static void DoIt(boolean close, boolean display) throws Exception
	{
		getName(1,close,display);
		getName(10,close,display);
		getName(108,close,display);
		getName(1088,close, display);
	}

	public static void main(String[] args) throws Exception
	{
		boolean close = Boolean.getBoolean("close");
		boolean display = Boolean.getBoolean("display");
		int cLoop = Integer.getInteger("count",20);
		int j;
		
		if (!close)
		{	connection = DriverManager.getConnection(db_string);
		}
		long startTime = System.nanoTime();
		for(j = 0; j < cLoop; ++j)
		{	DoIt(close, display);
		}
		long endTime = System.nanoTime();
		if (!close)
		{	connection.close();
			connection = null;
		}
		System.out.println(cLoop +" iterations executed in " + (endTime - startTime) / 1000000 + " milliseconds");
	}
}
