background image
<< Source code for AuthExampleEmbeddedSQLAuth.java | Create, update, and query table >>
<< Source code for AuthExampleEmbeddedSQLAuth.java | Create, update, and query table >>

The 08006 exception

Derby Developer's Guide
147
} catch (Exception ee) {
errorPrintAndExit(ee);
}
// Create and boot the database as user mary (who then becomes
// the database owner), set up users and then shut down the
// database
try {
System.out.println("Trying to connect to " + connectionURL);
conn = DriverManager.getConnection(connectionURL);
System.out.println("Connected to database " + connectionURL);
turnOnBuiltInUsers(conn);
// Close connection
conn.close();
System.out.println("Closed connection");
/* Shut down the database to make static properties take
* effect. Because the default connection mode is now
* noAccess, you must specify a user that has access. But
* because requireAuthentication and sqlAuthorization do not
* take effect until you restart the database, you do not
* need to specify a password.
*
* Database shutdown throws the 08006 exception to confirm
* success.
*/
try {
DriverManager.getConnection("jdbc:derby:" + dbName +
";user=mary;shutdown=true");
} catch (SQLException se) {
if ( !se.getSQLState().equals("08006") ) {
throw se;
}
}
System.out.println("Database shut down normally");
} catch (SQLException e) {
errorPrintAndExit(e);
}
// Restart database and confirm that unauthorized users cannot
// access it
connectionURL = "jdbc:derby:" + dbName;
// Try to log in with no username or password
try {
// connection attempt should fail
System.out.println("Trying to connect to " + connectionURL +
" without username or password");
conn = DriverManager.getConnection(connectionURL);
System.out.println(
"ERROR: Unexpectedly connected to database " + dbName);
cleanUpAndShutDown(conn);
} catch (SQLException e) {
if (e.getSQLState().equals("08004")) {
System.out.println("Correct behavior: SQLException: " +
e.getMessage());
} else {
errorPrintAndExit(e);
}
}
// Try to log in as a valid user with noAccess
try {
// connection attempt should fail
String newURL = connectionURL + ";user=sa;password=ajaxj3x9";
System.out.println("Trying to connect to " + newURL);
conn = DriverManager.getConnection(newURL);
System.out.println(