import java.sql.*;

public class SavePoints {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ClassNotFoundException
	{
		Connection connection=null;
		String[] imiona = { "Ala", "Basia", "Dusia", "Gienia", "Kasia" };
		Savepoint sp;
		boolean success = false;
		try
		{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=KursBD;user=sa;password=sql2005");
			connection.setAutoCommit( false );
			Statement stmt = connection.createStatement();
			stmt.executeUpdate( "delete from Names" );
			stmt.executeUpdate( "insert into Names values("+1+", '"+imiona[0]+"')" );
			stmt.executeUpdate( "insert into Names values("+2+", '"+imiona[1]+"')" );
			sp = connection.setSavepoint( "dwa_pierwsze" );
			stmt.executeUpdate( "insert into Names values("+3+", '"+imiona[2]+"')" );
			stmt.executeUpdate( "insert into Names values("+4+", '"+imiona[3]+"')" );
			connection.rollback( sp );
			stmt.executeUpdate( "insert into Names values("+5+", '"+imiona[4]+"')" );
			success = true;
		}
		catch (SQLException sqle)
		{ 
			sqle.printStackTrace();
		}
		finally
		{
			if ( connection != null )
			{
				if ( success )
				{
					try { connection.commit(); }
					catch( SQLException e )
					{
						try { connection.rollback(); }
						catch ( Exception e2 ) { e2.printStackTrace(); }						
					}
				}
				else
				{
					try { connection.rollback(); }
					catch ( Exception e ) { e.printStackTrace(); }						
				}
			}
			try { connection.close(); }
			catch ( Exception e ) { e.printStackTrace(); }
		}
	}
}
