/*

drop procedure sp_wstaw
create procedure sp_wstaw( @x int, @name varchar )
as
begin
  insert into names values( @x, @name );
end;


*/

import java.sql.*;

public class Wsady3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ClassNotFoundException
	{
		Connection connection=null;
		CallableStatement cstmt=null;
		int[] ile;
		try
		{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=KursBD;user=sa;password=sql2005");
			
			cstmt = connection.prepareCall( "{call sp_wstaw( ?, ? )}");
			cstmt.setInt( 1, 10 );
			cstmt.setString( 2, "Ala" );
			cstmt.addBatch();
			ile = cstmt.executeBatch();
			cstmt.close();
			for ( int i=0; i<ile.length; i++ )
			{
				System.out.println( "Ilość przetworzonych wierszy w instrukcji "+(i+1)+": "+ile[i] );				
			}
		}
		catch ( BatchUpdateException e )
		{
			e.printStackTrace();
			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(); }
		}
	}

}