
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.Properties;


public class SampleApp1
{
    /* the default framework is embedded*/
    public String framework = "embedded";
    public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    public String protocol = "jdbc:derby:";
    
    public String username = "";
    public String password = "";

    public static void main(String[] args)
    {
        new SampleApp1().go(args);
    }

    void go(String[] args)
    {
        
        System.out.println("SimpleApp starting in " + framework + " mode.");

        try
        {
            /*
               The driver is installed by loading its class.
               In an embedded environment, this will start up Derby, since it is not already running.
             */
            org.apache.derby.jdbc.EmbeddedSimpleDataSource ds = null;
            Connection conn = null;
            Properties props = new Properties();
            props.put("user", username);
            props.put("password", password);
          
			Class.forName(driver).newInstance();
			System.out.println("Loaded the appropriate driver.");

			conn = DriverManager.getConnection(protocol + "24k;", props);

            System.out.println("Connected to 24k");

            conn.setAutoCommit(false);

			/*
			   Creating a statement lets us issue commands against
			   the connection.
			*/
            Statement s = conn.createStatement();


            ResultSet rs = s.executeQuery("SELECT firstname, lastname FROM USERS WHERE uid = 1001");

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 300)
            {
                throw new Exception("Wrong row returned");
            }

            if (!rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

            if (rs.getInt(1) != 1910)
            {
                throw new Exception("Wrong row returned");
            }

            if (rs.next())
            {
                throw new Exception("Wrong number of rows");
            }

			/*
			   We release the result and statement resources.
			 */
            rs.close();
            s.close();
            System.out.println("Closed result set and statement");

			/*
			   We end the transaction and the connection.
			 */
            conn.commit();
            conn.close();
            System.out.println("Committed transaction and closed connection");

            boolean gotSQLExc = false;

            if (framework.equals("embedded"))
            {
				try
				{
					DriverManager.getConnection("jdbc:derby:;shutdown=true");
				}
				catch (SQLException se)
				{
					gotSQLExc = true;
				}

                if (!gotSQLExc)
                {
                    System.out.println("Database did not shut down normally");
                }
                else
                {
                    System.out.println("Database shut down normally");
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("exception thrown:");

			e.printStackTrace();
        }

        System.out.println("SampleApp finished");
    }
}