return con; } As you can see, there isn t a lot going on here for the SimpleText driver; remember that we need to keep the size of the Driver class implementation as small as possible. To aid in this, all of the code required to perform the database connection resides in the Connection class, which we ll discuss next. Connection The Connection class represents a session with the data source. From here, you can create Statement objects to execute SQL statements and gather database statistics. Depending upon the database that you are using, multiple connections may be allowed for each driver. For the SimpleText driver, we don t need to do anything more than actually connect to the database. In fact, there really isn t a database at all just a bunch of text files. For typical database drivers, some type of connection context will be established, and default information will be set and gathered. During the SimpleText connection initialization, all that we need to do is check for a read-only condition (which can only occur within untrusted applets) and any properties that are supplied by the application, as shown in Listing 10.18. Listing 10.18 SimpleText connection initialization. public void initialize( Driver driver, java.util.Properties info) throws SQLException { // Save the owning driver object ownerDriver = driver; // Get the security manager and see if we can write to a file. // If no security manager is present, assume that we are a trusted // application and have read/write privileges. canWrite = false; SecurityManager securityManager = System.getSecurityManager (); if (securityManager != null) { try { // Use some arbitrary file to check for file write privileges securityManager.checkWrite (”SimpleText_Foo”); // Flag is set if no exception is thrown canWrite = true; } // If we can’t write, an exception is thrown. We’ll catch // it and do nothing. catch (SecurityException ex) {
Find the right website host for you, offering a directory of website hosts, free domain name registration tool, hosting reviews, web hosting ratings and articles for beginners, and tons of other resources, check web hosting ratings for more information.