Java Reference
In-Depth Information
Tutorial: Breaking the Code, Drivers, and Connections
Accessing databases can be very frustrating, especially when things go wrong because you really do not know what
is going on. For instance, you use classes (like Class and DriverManager ) with no idea of how they work. Often new
programmers feel that if they knew how these classes worked, they could solve the problems that do occur. However,
understanding how they work will not help and, trust me, you do not want to know how they work! How a connection
to the database is made is complicated and understanding the process will not help solve the problems that are likely
to arise. Usually things go wrong because of simple programming mistakes and typos, not because of the special
database classes or the DBMS you are trying to access. We will replicate the most common mistakes and highlight
some good testing techniques so you will be able to recognize and solve these common problems quickly.
In DBAccess, change the first letter in the text of the String driver from lowercase
to uppercase.
1.
2.
Run DBAccess as a Java application.
The following message will appear in the Console:
Driver class not found: java.lang.ClassNotFoundException:
Xxx.xxxxx.xxxxDriver
where Xxx.xxxxx.xxxxDriver is the incorrectly capitalized driver name.
“Driver class not found” is a straightforward message, especially in this case, because we incorrectly specified
the driver class name. The lesson is: be careful of capitalization, spelling, and the use of special characters (periods,
forward slashes, colons, etc.) because when problems occur, these are the usual culprits.
3.
Change the first letter of the driver text back to lowercase.
4.
Comment out the first six lines of the DBAccess constructor that register the driver
(including the try / catch statements).
5.
Run DBAccess as a Java application.
Notice that this time we get the error message:
No connection: java.sql.SQLException: No suitable driver
“No suitable driver” is not very descriptive, but it means that the JVM could not find the correct driver. When the
connection is attempted, the JVM searches the registered drivers for the correct driver. In this case, we specified the
driver name correctly but did not register it, so the JVM cannot find the driver and an exception is thrown.
6.
Uncomment the six lines.
In the String url, delete the last character.
7.
8.
Run DBAccess as a Java application.
The following messages are displayed (with Xxxxxx being unique text for each DBMS):
Driver class found!
No connection: java.sql.SQLException: Xxxxxx
Notice that the driver was successfully registered but no connection was created because we did not specify the
correct location. This error will also occur if the DBMS you are trying to access is not available. A DBMS may not be
available for a variety of reasons. For example, the communication link between your computer and the DBMS could
be down, the database could be off line for maintenance work, or the computer where the database resides is turned
off. Usually this error is generated because the programmer has simply mistyped the IP address or URL.
 
Search WWH ::




Custom Search