/*
drop table names;

create table names ( id int primary key, name varchar2(20) );

select * from names;
*/

import java.sql.*;

public class Wsady2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ClassNotFoundException
	{
		Connection connection=null;
		int[] ile;
		String[] imiona = { "Ala", "Basia", "Dusia", "Gienia", "Kasia" };
		try
		{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=KursBD;user=sa;password=sql2005");
			PreparedStatement stmt = connection.prepareStatement( "insert into names values ( ?, ? )" );
			stmt.setString( 2, "VVV" );
			for ( int i=0; i<imiona.length; i++ )
			{
				stmt.setInt( 1, (i+1) );
				//stmt.setString( 2, imiona[i] );
				stmt.addBatch();
				//stmt.clearParameters();
				// czy powyższe jest konieczne? Odkomentowujemy wiersz 26, zakomentowujemy wiersz 30 i sprawdzamy 
			}
			System.out.println( PreparedStatement.SUCCESS_NO_INFO );
			ile = stmt.executeBatch();
			for ( int i=0; i<ile.length; i++ )
			{
				System.out.println( "Ilość przetworzonych wierszy w instrukcji "+(i+1)+": "+ile[i] );				
			}
			//PreparedStatement.
			stmt.close();
		}
		catch ( BatchUpdateException e )
		{
			int[] count=e.getUpdateCounts();
			for ( int i=0; i<count.length; i++ )
			{
				System.out.println( "Błąd: ilość przetworzonych wierszy w instrukcji "+(i+1)+": "+count[i] );				
			}			
		}
		catch (SQLException sqle)
		{ 
			sqle.printStackTrace();
			while (sqle != null)
			{
				String logMessage = "\n SQL Error: "+
					sqle.getMessage() + "\n\t\t"+
					"Error code: "+sqle.getErrorCode() + "\n\t\t"+
					"SQLState: "+sqle.getSQLState()+"\n";
				System.out.println(logMessage);
				sqle = sqle.getNextException();
			}
		}
		finally
		{
			try { if ( connection != null ) connection.close(); }
			catch ( Exception e ) { e.printStackTrace(); }
		}
	}

}
