db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel A Keefe <dan.ke...@convergys.com>
Subject RE: problem with embedded driver
Date Tue, 19 Apr 2011 21:35:08 GMT
no.  ij is strictly a manual tool.

dk

________________________________________
From: steve lescure [stevelescure@yahoo.com]
Sent: Tuesday, April 19, 2011 4:36 PM
To: Derby Discussion
Subject: RE: problem with embedded driver

ok - i see some examples of using "ij" to manually create the connection, that makes sense.


i thought if i were using the embedded driver it would automatically start derby.....

would i need to somehow call "ij" in my java code?

package ondemand;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Properties;

public class DemandDatabase {

    private String framework = "embedded";
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    private String protocol = "jdbc:derby:";

    public DemandDatabase() {
        System.out.println("in constructor ");
    }

    public ArrayList go() // void go(String[] args)
    {
        System.out.println("about to load the driver in " + framework + " mode");
        loadDriver();
        Connection conn = null;

        ArrayList statements = new ArrayList();
        ArrayList jobnames = new ArrayList();

        Statement s = null;
        ResultSet rs = null;

        try {
            Properties props = new Properties(); // connection properties
            props.put("user", "nbuser");
            props.put("password", "nbuser");
            String dbName = "//localhost:1527/users"; // the name of the database
            conn = DriverManager.getConnection(protocol + dbName
                    + ";create=true", props);
            System.out.println("Connected to database " + dbName);

            conn.setAutoCommit(true);
            s = conn.createStatement();
            statements.add(s);
            rs = s.executeQuery(
                    "SELECT JOBNAME, USERID FROM APP.JOBS");
            String txtJobname;
            String txtUserID;

            while (rs.next()) {
                txtJobname = rs.getString("JOBNAME");
                System.out.println("value of jobname is " + txtJobname);
                txtUserID = rs.getString("USERID");
                System.out.println("value of User ID is " + txtUserID);
                jobnames.add(txtJobname);
            }

            try {
                // the shutdown=true attribute shuts down Derby
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException se) {
                if (((se.getErrorCode() == 50000)
                        && ("XJ015".equals(se.getSQLState())))) {
                    // we got the expected exception
                    System.out.println("Derby shut down normally");
                    // Note that for single database shutdown, the expected
                    // SQL state is "08006", and the error code is 45000.
                } else {
                    // if the error code or SQLState is different, we have
                    // an unexpected exception (shutdown failed)
                    System.out.println("Derby did not shut down normally");
                    printSQLException(se);
                }
            }
        } catch (SQLException sqle) {
            printSQLException(sqle);
        } finally {
            // release all open resources to avoid unnecessary memory usage
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
            } catch (SQLException sqle) {
                printSQLException(sqle);
            }

            // Statements and PreparedStatements
            int i = 0;
            while (!statements.isEmpty()) {
                // PreparedStatement extend Statement
                Statement st = (Statement) statements.remove(i);
                try {
                    if (st != null) {
                        st.close();
                        st = null;
                    }
                } catch (SQLException sqle) {
                    printSQLException(sqle);
                }
            }
            //Connection
            try {
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException sqle) {
                printSQLException(sqle);
            }
        }
        return jobnames;
    }

    private void loadDriver() {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            System.out.println("Loaded the appropriate driver");
        } catch (ClassNotFoundException cnfe) {
            System.err.println("\nUnable to load the JDBC driver " + driver);
            System.err.println("Please check your CLASSPATH.");
            cnfe.printStackTrace(System.err);
        } catch (InstantiationException ie) {
            System.err.println(
                    "\nUnable to instantiate the JDBC driver " + driver);
            ie.printStackTrace(System.err);
        } catch (IllegalAccessException iae) {
            System.err.println(
                    "\nNot allowed to access the JDBC driver " + driver);
            iae.printStackTrace(System.err);
        }
    }

    private void reportFailure(String message) {
        System.err.println("\nData verification failed:");
        System.err.println('\t' + message);
    }

    public static void printSQLException(SQLException e) {
        // Unwraps the entire exception chain to unveil the real cause of the
        // Exception.
        while (e != null) {
            System.err.println("\n----- SQLException -----");
            System.err.println("  SQL State:  " + e.getSQLState());
            System.err.println("  Error Code: " + e.getErrorCode());
            System.err.println("  Message:    " + e.getMessage());
            // for stack traces, refer to derby.log or uncomment this:
            //e.printStackTrace(System.err);
            e = e.getNextException();
        }
    }
}

NOTICE:  The information contained in this electronic mail transmission is intended by Convergys
Corporation for the use of the named individual or entity to which it is directed and may
contain information that is privileged or otherwise confidential.  If you have received this
electronic mail transmission in error, please delete it from your system without copying or
forwarding it, and notify the sender of the error by reply email or by telephone (collect),
so that the sender's address records can be corrected.

Mime
View raw message