Archive for December, 2007

Monday, December 31st, 2007

containing all of the information about the column as the data value. So, for all ResultSets that are generated, I create a Hashtable of column information that is then used by the ResultSet object and the ResultSetMetaData object to describe each column. Listing 10.19 shows the SimpleTextColumn class that is used to hold this information for each column. Listing 10.19 The SimpleTextColumn class. package jdbc.SimpleText; public class SimpleTextColumn extends Object { //———————————————————————- — // Constructor //———————————————————————- — public SimpleTextColumn( String name, int type, int precision) { this.name = name; this.type = type; this.precision = precision; } public SimpleTextColumn( String name, int type) { this.name = name; this.type = type; this.precision = 0; } public SimpleTextColumn( String name) { this.name = name; this.type = 0; this.precision = 0; } public String name; public int type; public int precision; public boolean searchable; public int colNo; public int displaySize; public String typeName; } Note that I have used several constructors to set up various default information, and that all of the attributes are public. To follow object-oriented design, I should have provided a
You need web hosting, easy to use web template and great support. What else could I ask for?All of our reseller accounts include free web hosting templates just check web hosting templates for more information.

Sunday, December 30th, 2007

information about what is supported, how it behaves, and what type of information exists in the database. The getMetaData method creates a DatabaseMetaData object which provides us with this wealth of information. DatabaseMetaData At over 130 methods, the DatabaseMetaData class is by far the largest. It supplies information about what is supported and how things are supported. It also supplies catalog information such as listing tables, columns, indexes, procedures, and so on. Because the JDBC API specification does an adequate job of explaining the methods contained in this class, and most of them are quite straightforward, we ll just take a look at how the SimpleText driver implements the getTables catalog method. But first, let s review the basic steps needed to implement each of the catalog methods (that is, those methods that return a ResultSet): 1. Create the result columns, which includes the column name, type, and other information about each of the columns. You should perform this step regardless of whether the database supports a given catalog function (such as stored procedures). I believe that it is much better to return an empty result set with only the column information than to raise an exception indicating that the database does not support the function. The JDBC specification does not currently address this issue, so it is open for interpretation. 2. Retrieve the catalog information from the database. 3. Perform any filtering necessary. The application may have specified the return of only a subset of the catalog information. You may need to filter the information in the JDBC driver if the database system doesn t. 4. Sort the result data per the JDBC API specification. If you are lucky, the database you are using will sort the data in the proper sequence. Most likely, it will not. In this case, you will need to ensure that the data is returned in the proper order. 5. Return a ResultSet containing the requested information. The SimpleText getTables method will return a list of all of the text files in the catalog (directory) given. If no catalog is supplied, the default directory is used. Note that the SimpleText driver does not perform all of the steps shown previously; it does not provide any filtering, nor does it sort the data in the proper sequence. You are more than welcome to add this functionality. In fact, I encourage it. One note about column information: I prefer to use a Hashtable containing the column number as the key, and a class
Our facility is located in Orlando, Florida. Our Orlando web hosting data center gives you assurance that your website will work smoothly . Check more in Orlando Web Hosting.

Saturday, December 29th, 2007

Now consider the corresponding initialize method in the Statement class: public void initialize( SimpleTextConnection con) throws SQLException { // Save the owning connection object ownerConnection = con; } Which module will you compile first? You can t compile the Connection class until the Statement class has been compiled, and you can t compile the Statement class until the Connection class has been compiled. This is a circular dependency. Of course, the Java compiler does allow multiple files to be compiled at once, but some build environments do not support circular dependency. I have solved this problem in the SimpleText driver by defining some simple interface classes. In this way, the Statement class knows only about the general interface of the Connection class; the implementation of the interface does not need to be present. Our modified initialize method looks like this: public void initialize( SimpleTextIConnection con) throws SQLException { // Save the owning connection object ownerConnection = con; } Note that the only difference is the introduction of a new class, SimpleTextIConnection, which replaces SimpleTextConnection. I have chosen to preface the JDBC class name with an I to signify an interface. Here s the interface class: public interface SimpleTextIConnection extends java.sql.Connection { String[] parseSQL(String sql); Hashtable getTables(String directory, String table); Hashtable getColumns(String directory, String table); String getDirectory(String directory); } Note that our interface class extends the JDBC class, and our Connection class implements this new interface. This allows us to compile the interface first, then the Statement, followed by the Connection. Say good-bye to your circular dependency woes. Now, back to the Statement objects. The prepareStatement and prepareCall methods of the Connection object both require an SQL statement to be provided. This SQL statement should be pre-compiled and stored with the Statement object. If any errors are present in the SQL statement, an exception should be raised, and the Statement object should not be created. Tell Me About Yourself One of the most powerful aspects of the JDBC specification (which was inherited from X/Open) is the ability for introspection. This is the process of asking a driver for
Have all the commercials and ads about web hosting companies given you headache? Relax now.Our recommendation is web hosting comparisons.

Friday, December 28th, 2007

} } else { canWrite = true; } // Set our initial read-only flag setReadOnly(!canWrite); // Get the directory. It will either be supplied in the property // list, or we’ll use our current default. String s = info.getProperty(”Directory”); if (s == null) { s = System.getProperty(”user.dir”); } setCatalog(s); } Creating Statements From the Connection object, an application can create three types of Statement objects. The base Statement object is used for executing SQL statements directly. The PreparedStatement object (which extends Statement) is used for pre-compiling SQL statements that may contain input parameters. The CallableStatement object (which extends PreparedStatement) is used to execute stored procedures that may contain both input and output parameters. For the SimpleText driver, the createStatement method does nothing more than create a new Statement object. For most database systems, some type of statement context, or handle, will be created. One thing to note whenever an object is created in a JDBC driver: Save a reference to the owning object because you will need to obtain information (such as the connection context from within a Statement object) from the owning object. Consider the createStatement method within the Connection class: public Statement createStatement() throws SQLException { if (traceOn()) { trace(”Creating new SimpleTextStatement”); } // Create a new Statement object SimpleTextStatement stmt = new SimpleTextStatement(); // Initialize the statement stmt.initialize(this); return stmt; }
If you need complete web hosting solution you come to right place,try mac web hosting services.

Thursday, December 27th, 2007

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.

Thursday, December 27th, 2007

Listing 10.17 Connecting to the database. //———————————————————————- — // connect - JDBC API // // Try to make a database connection to the given URL. // The driver should return “null” if it realizes it is the wrong kind // of driver to connect to the given URL. This will be common, as when // the JDBC driver manager is asked to connect to a given URL, it passes // the URL to each loaded driver in turn. // // The driver should raise an SQLException if it is the right // driver to connect to the given URL, but has trouble connecting to // the database. // // The java.util.Properties argument can be used to pass arbitrary // string tag/value pairs as connection arguments. // Normally, at least “user” and “password” properties should be // included in the Properties. // // url The URL of the database to connect to. // // info a list of arbitrary string tag/value pairs as // connection arguments; normally, at least a “user” and // “password” property should be included. // // Returns a Connection to the URL. //———————————————————————- — public Connection connect( String url, java.util.Properties info) throws SQLException { if (traceOn()) { trace(”@connect (url=” + url + “)”); } // Ensure that we can understand the given URL if (!acceptsURL(url)) { return null; } // For typical JDBC drivers, it would be appropriate to check // for a secure environment before connecting, and deny access // to the driver if it is deemed to be unsecure. For the // SimpleText driver, if the environment is not secure, we will // turn it into a read-only driver. // Create a new SimpleTextConnection object SimpleTextConnection con = new SimpleTextConnection(); // Initialize the new object. This is where all of the // connection work is done. con.initialize(this, info);
Maybe you are looking hosting for companies or individuals who want a basic internet presence at budget price with no frills.From our experience you should check Budget Web Hosting part.

Tuesday, December 25th, 2007

// discover what properties it should prompt a human for in order to get // enough information to connect to a database. Note that depending on // the values the human has supplied so far, additional values may become // necessary, so it may be necessary to iterate though several calls. // to getPropertyInfo. // // url The URL of the database to connect to. // // info A proposed list of tag/value pairs that will be sent on // connect open. // // Returns an array of DriverPropertyInfo objects describing possible // properties. This array may be an empty array if no // properties are required. //———————————————————————- — public DriverPropertyInfo[] getPropertyInfo( String url, java.util.Properties info) throws SQLException { DriverPropertyInfo prop[]; // Only one property required for the SimpleText driver, the // directory. Check the property list coming in. If the // directory is specified, return an empty list. if (info.getProperty(”Directory”) == null) { // Setup the DriverPropertyInfo entry prop = new DriverPropertyInfo[1]; prop[0] = new DriverPropertyInfo(”Directory”, null); prop[0].description = “Initial text file directory”; prop[0].required = false; } else { // Create an empty list prop = new DriverPropertyInfo[0]; } return prop; } Let s Get Connected Now that we can identify a driver to provide services for a given URL and get a list of the required and optional parameters necessary, it s time to establish a connection to the database. The connect method does just that, as shown in Listing 10.17, by taking a URL and connection property list and attempting to make a connection to the database. The first thing that connect should do is verify the URL (by making a call to acceptsURL). If the URL is not supported by the driver, a null value will be returned. This is the only reason that a null value should be returned. Any other errors during the connect should throw an SQLException.
Our unmatched NT experience allows us to provide the most reliable and affordable hosting for our customers, just check NT Web Hosting services.

Monday, December 24th, 2007

// Get the property information DriverPropertyInfo info[] = d.getPropertyInfo(url, props); // Just dump them out System.out.println(”Number of properties: ” + info.length); for (int i=0; i < info.length; i++) { System.out.println("nProperty " + (i + 1)); System.out.println("Name: " + info[i].name); System.out.println("Description: " + info[i].description); System.out.println("Required: " + info[i].required); System.out.println("Value: " + info[i].value); System.out.println("Choices: " + info[i].choices); } } catch (SQLException ex) { System.out.println ("nSQLException(s) caughtn"); // Remember that SQLExceptions may be chained together while (ex != null) { System.out.println("SQLState: " + ex.getSQLState()); System.out.println("Message: " + ex.getMessage()); System.out.println (""); ex = ex.getNextException (); } } } } Listing 10.15 produces the following output: Number of properties: 1 Property 1 Name: Directory Description: Initial text file directory Required: false Value: null Choices: null It doesn t take a lot of imagination to envision an application or applet that gathers the property information and prompts the user in order to connect to the database. The actual code to implement the getPropertyInfo method for the SimpleText driver is very simple, as shown in Listing 10.16. Listing 10.16 Implementing the getPropertyInfo method. //---------------------------------------------------------------------- -- // getPropertyInfo - JDBC API // // The getPropertyInfo method is intended to allow a generic GUI tool to
Adult web hosting has rich experience in providing unique solutions for the customer’s needs, just check frontpage web hosting services.

Monday, December 24th, 2007

// Look for the colon that separates the subname // from the subprotocol (or the fact that there // is no subprotocol at all). if (url.startsWith(”:”)) { subname = url.substring(subProtocol.length()); } else if (url.length() == 0) { subname = “”; } } } } return subname; } Driver Properties Connecting to a JDBC driver with only a URL specification is great, but the vast majority of the time, a driver will require additional information in order to properly connect to a database. The JDBC specification has addressed this issue with the getPropertyInfo method. Once a Driver has been instantiated, an application can use this method to find out what required and optional properties can be used to connect to the database. You may be tempted to require the application to embed properties within the URL subname, but by returning them from the getPropertyInfo method, you can identify the properties at runtime, giving a much more robust solution. Listing 10.15 shows an application that loads the SimpleText driver and gets the property information. Listing 10.15 Using the getPropertyInfo method to identify properties at runtime. import java.sql.*; class PropertyTest { public static void main(String args[]) { try { // Quick way to create a driver object java.sql.Driver d = new jdbc.SimpleText.SimpleTextDriver(); String url = “jdbc:SimpleText”; // Make sure we have the proper URL if (!d.acceptsURL(url)) { throw new SQLException(”Unknown URL: ” + url); } // Setup a Properties object. This should contain an entry // for all known properties to this point. Properties that // have already been specified in the Properties object will // not be returned by getPropertyInfo. java.util.Properties props = new java.util.Properties();
Need a managed web hosting provider to help maintain your website? Our web hosting service is the preferred choice of thousands of demanding customers.

Sunday, December 23rd, 2007

throws SQLException { if (traceOn()) { trace(”@acceptsURL (url=” + url + “)”); } boolean rc = false; // Get the subname from the url. If the url is not valid for // this driver, a null will be returned. if (getSubname(url) != null) { rc = true; } if (traceOn()) { trace(” ” + rc); } return rc; } //———————————————————————- — // getSubname // Given a URL, return the subname. Returns null if the protocol is // not “jdbc” or the subprotocol is not “simpletext.” //———————————————————————- — public String getSubname( String url) { String subname = null; String protocol = “JDBC”; String subProtocol = “SIMPLETEXT”; // Convert to uppercase and trim all leading and trailing // blanks. url = (url.toUpperCase()).trim(); // Make sure the protocol is jdbc: if (url.startsWith(protocol)) { // Strip off the protocol url = url.substring (protocol.length()); // Look for the colon if (url.startsWith(”:”)) { url = url.substring(1); // Check the subprotocol if (url.startsWith(subProtocol)) { // Strip off the subprotocol, leaving the subname url = url.substring(subProtocol.length()); //———————————————————————- — public boolean acceptsURL( String url)
Check our reliable web hosting section. Most often, a reliable protocol is also connection-oriented. However, this is not always so. For example, TCP/IP is a connection-oriented protocol, with the virtual circuit ID consisting of source and destination IP addresses and port numbers. However, there are also unreliable protocols that are connection-oriented as well. These include ATM and Frame Relay, on which 90% or more of all Internet traffic is passed.