[ Back | Previous | Next ]

Using ODBC-JDBC with java.sql.*

Package:
java.sql.*
Product:
JDK
Release:
1.1.x
Related Links:
General
General
General
General
CallableStatement
Comment:
1.
    JDK 1.1
    Where to get Java Development Kit? 
    http://java.sun.com, version 1.1 incudes the java.sql.* and sun.jdbc.odbc.* drivers. 
    Install them under the d:/java directory. 

 2.
    JDBC-ODBC Bridge
          Consists of two packages. 
       1.win95; 32-bits odbc drivers. Are delivered with the MS-Office CD. Run setup on CD and goto Add/Remove components. Select
          Converters, Filters, and Data Access, and select all. Press Continue. 
       2.jdbc-odbc brigde; Intersolv has made an jdbc-odbc brigde for general use with ms-access. The bridge consists of two .dll's
          jdbcodbc.dll, jdbcodbc_g.dll and Netscape_jdbcodbc.dll. After downloading proceed with step 3.

 3.
    SYSTEM ENVIRONMENT
       1.Copy the JdbcOdbc.zip file into your Java home directory (i.e. c:\java). This
          file contains all of the JDBC class files for the driver as well as the operating
          system dependent libraries and sample source code. 
       2.Unzip the JdbcOdbc.zip file, using the directory names stored in the ZIP file (be
          sure to use an unzip program that preserves long file names, such as WinZIP). 

          This process creates the following subdirectory structure beneath your Java
          home directory: 

                Directory            Contains
                ---------            ----------------------------------------------
                java\sql             The JDBC API classes provided by JavaSoft
                sun\jdbc\odbc        The JDBC-ODBC Bridge classes, libraries, and
                                     sample source code
             

       3.Copy the JdbcOdbc.dll library (in sun\jdbc\odbc\win32) to a directory on your
          library path (i.e. c:\windows\system). 
       4.If you will be using the JDBC-ODBC Bridge with Netscape 3.0, copy the
          Netscape_JdbcOdbc.dll library (in sun\jdbc\odbc\win32) in the
          program\java\bin subdirectory under the Netscape 3.0 home directory.
          Note that if you are using the JDBC-ODBC Bridge with Netscape 3.0, the
          Java home directory is program\java\classes under your Netscape 3.0
          home directory. Applets must be loaded from your CLASSPATH or from
          the program\java\classes directory in order to use the JDBC-ODBC Bridge.

    Back to top
 4.
    32-BITS ODBC DRIVER CONFIGURATION
       1.In windows 95 goto your control panel and select the 32-bits ODBC Driver. 
                     

          Now add System DSN (Data Source Name) by pressing System DSN and
          Add. Select Microsoft Access Driver (*.mbd). Fill out the form; Data Source
          Name, Description and press select to make link to the actual database on your
          harddrive. 

          The Data Source Name we'll use in our Java Source Code to make a
          connection with the ODBC. Our settings are: 

          data source name = Noorderwind (Northwind)
          description = default database for ms-access
          select path = c:/msoffice/access/nwind.mdb
                  

 5.
    Java Code For Accessing Database
       1.Making the link to the jdbcodbc.class which will talk with the right *.dll. 

               public NorthWind() {

                  // Load the JDBC-ODBC bridge driver 
                          try {
                                  Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
                          } catch( ClassNotFoundException ee) {   
                                  ee.printStackTrace();
                          }
              }
                  

       2.open a data source name by means of the jdbcodbcdriver. 

          static void open() throws SQLException {

                  // ODBC data source name 
                  String dsn = "jdbc:odbc:Nooderwind"; 
                  String user = "admin"; 
                  String password = "";

                  // Connect to the database 
                  con = DriverManager.getConnection(dsn, user, password);

                  // Shut off autocommit 
                  con.setAutoCommit(false);

              }


       3.executing a query on the opened connection.

          static void select(String whereClause)  {

              Statement stmt; // SQL statement object 
              String query;   // SQL select string 
              ResultSet rs;   // SQL query results 
              boolean more;   // "more rows found" switch      
              String v1, v2, v3; // Temporary storage results     

              results = new Vector( 10 );

              query = "SELECT EmployeeId, LastName, FirstName, Title "
                      + "FROM Employees "
                      + whereClause;
                                                
                      
                  try {
                 stmt = con.createStatement();
                 rs = stmt.executeQuery(query);
              
                 // Check to see if any rows were read
                 more = rs.next(); 
                  if (!more) {

                      System.out.println("No rows found."); 
                      return;

                  }

                  // Loop through the rows retrieved from the query
                  while (more) {

                                  v1 = "ID: " + rs.getInt("EmployeeId");
                                  v2 = "Name: " + rs.getString("FirstName") + " " + rs.getString("LastName");
                                  v3 = "Title: " + rs.getString("Title");

                      System.out.println( v1 );
                      System.out.println( v2 ); 
                      System.out.println( v3 ); 
                      System.out.println("");    

                                  results.addElement( v1 + "\n" + v2 + "\n" + v3 + "\n");
                      more = rs.next();

                  }

                  rs.close(); 
                  stmt.close();
                          } catch (SQLException e) {
                                  System.out.println("" + results.size() + " results where found.");
                          }