Java Tutorial - Java Script : The JDBC-ODBC Bridge

Java Tutorial - Java Script :

The JDBC-ODBC Bridge

Unlike other inputoutput classes in Java, JDBC doesn’t use a filename to identify a data file and use its contents. Instead, a tool such as the ODBC Data Source Administrator is used to name the ODBC source and indicate the file folder where it can be found. In the ODBC Data Source Administrator, click the User DSN tab to see a list of datasources that are available. To add a new one associated with world20.mdb (or your own database), click the Add button, choose an ODBC driver, and then click the Finish button. A Setup window opens that you can use to provide a name, short description, and other information about the database. Click the Select button to find and choose the database file.Figure 18.3 shows the Setup window used to set up world20.mdb as a data source in the  ODBC Data Source Administrator.After a database has been associated with an ODBC data source, working with it in a Java program is relatively easy if you are conversant with SQL. The first task in a JDBC program is to load the driver (or drivers) that will be used to connect to a data source. A driver is loaded with the Class.forName(String) method. Class, part of the java.lang package, can be used to load classes into the Java interpreter. The forName(String) method loads the class named by the specified string. A ClassNotFoundException can be thrown by this method. All programs that use an ODBC data source use sun.jdbc.odbc.JdbcOdbcDriver, the JDBC-ODBC bridge driver included with Java. Loading this class into a Java interpreter requires the following statement:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
After the driver has been loaded, you can establish a connection to the data source by using the DriverManager class in the java.sql package. The getConnection(String, String, String) method of DriverManager can be used to set up the connection. It returns a reference to a Connection object representing an active data connection. The three arguments of this method are as follows:

·         A name identifying the data source and the type of database connectivity used to reach it
·         A username
·         A password
The last two items are needed only if the data source is secured with a username and a password. If not, these arguments can be null strings (“”). The name of the data source is preceded by the text jdbc:odbc: when using the JDBCODBC bridge, which indicates the type of database connectivity in use. The following statement could be used to connect to a data source called Payroll with a username of “Doc” and a password of “1rover1”:
Connection payday = DriverManager.getConnection( “jdbc:odbc:Payroll”, “Doc”, “1rover1”);
After you have a connection, you can reuse it each time you want to retrieve or store information from that connection’s data source.The getConnection() method and all others called on a data source throw SQLException errors if something goes wrong as the data source is being used. SQL has its own error messages, and they are passed along as part of SQLException objects.