Java Reference
In-Depth Information
Example 17−5: RemoteDBBankServer.java (continued)
String user = p.getProperty("user", "");
String password = p.getProperty("password", "");
// Load the database driver class
Class.forName(driver);
// Connect to the database that stores our accounts
Connection db = DriverManager.getConnection(database,
user, password);
// Configure the database to allow multiple queries and updates
// to be grouped into atomic transactions
db.setAutoCommit(false);
db.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
// Create a server object that uses our database connection
RemoteDBBankServer bank = new RemoteDBBankServer(db);
// Read a system property to figure out how to name this server.
// Use "SecondRemote" as the default.
String name = System.getProperty("bankname", "SecondRemote");
// Register the server with the name
Naming.rebind(name, bank);
// And tell everyone that we're up and running.
System.out.println(name + " is open and ready for customers.");
}
catch (Exception e) {
System.err.println(e);
if (e instanceof SQLException)
System.err.println("SQL State: " +
((SQLException)e).getSQLState());
System.err.println("Usage: java [-Dbankname=<name>] " +
"com.davidflanagan.examples.sql.RemoteDBBankServer " +
"[<dbpropsfile>]");
System.exit(1);
}
}
}
Exercises
17-1. Before you begin using JDBC, you must first have a database server and a
JDBC driver for it, and you must know how to administer the server in order
to do such things as create new databases. If you are not already an experi-
enced database programmer, learning to do all this is more difficult than
actually programming with JDBC. For this first exercise, therefore, obtain
and install a database server if you don't already have one. Obtain and
install a JDBC driver for it. Read the documentation for both the server and
the driver. Learn the basics of the SQL language, if you don't already know
it, and make a note of what SQL subset or SQL extensions are supported by
your server and JDBC driver.
Search WWH ::




Custom Search