Java Reference
In-Depth Information
Loading a JDBC™ Database Driver
To use a specific database with JDBC™ requires having the appropriate
driver available. The driver typically is provided by the database vendor. Before
vendors began providing JDBC™ drivers for their databases, the availability of
ODBC drivers was leveraged by Java with the introduction of the JDBC-ODBC
Bridge. The JDBC-ODBC Bridge is a JDBC™ driver that makes calls to the
ODBC driver for access to the database engine. This was the first type of driver
available for general use and is provided with JDBC™.
As indicated earlier, a utility program needs to be created first in order to
create the database structure and initial data for the database. Figure 11-17
displays the code for the utility program, MakeDB.java.
1 /*
2
Chapter 11: The MakeDB Class
3
Programmer: Michael Mick
4
Date:
December 12, 2007
5
Filename:
MakeDB.java
6
Purpose:
To build an initial database for the StockTracker application
7 */
8
9
import java.sql.*;
10
import java.io.*;
11
12
public class MakeDB
13
{
14
public static void main ( String [] args ) throws Exception
15
{
16
Class .forName ( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
17
18
String url = "jdbc:odbc:StockTracker" ;
19
20
Connection con = DriverManager .getConnection ( url ) ;
21
Statement stmt = con.createStatement () ;
22
23
// The following code deletes each index and table, if they exist.
24
// If they do not exist, a message is displayed and execution continues.
25
System .out.println ( "Dropping indexes & tables ..." ) ;
26
27
try
28
{
29
stmt.executeUpdate ( "DROP INDEX PK_UserStocks ON UserStocks" ) ;
30
31
}
32
catch ( Exception e )
33
{
34
System .out.println ( "Could not drop primary key on UserStocks table: "
35
+ e.getMessage ()) ;
36
}
37
38
try
39
{
40
stmt.executeUpdate ( "DROP TABLE UserStocks" ) ;
41
}
42
catch ( Exception e )
43
{
44
System .out.println ( "Could not drop UserStocks table: "
45
+ e.getMessage ()) ;
46
}
FIGURE 11-17
(continued)
Search WWH ::




Custom Search