Real'sHowTo AddThis Feed Button
Custom Search

Connect to a database via JDBC-ODBCTag(s): JDBC


You have to keep in mind that the bridge JDBC-ODBC is only useful in an Application, you can't use it with JAVA Applet because ODBC requires some DLL on the client machine (forbidden for security reason).
import java.net.URL;
import java.sql.*;

class JDBCapp  {

  static Connection theConn;

  public static void main (String args[]) {
    try {
      // connection to an ACCESS MDB
      theConn = MyConnection.getConnection();

      ResultSet rs;
      Statement stmt;
      String sql;

      sql =  "select objet from Email";
      stmt = theConn.createStatement();
      rs = stmt.executeQuery(sql);

      while (rs.next()) {
         System.out.println(rs.getString("objet"));
      }
      rs.close();
      stmt.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
      try {
        if (theConn != null) theConn.close();
      }
      catch (Exception e) {
      }
    }

    }
  }

class MyConnection {
  public static Connection getConnection() throws Exception {
    Driver d = (Driver)Class.forName
     ("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    Connection c = DriverManager.getConnection(
     "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/tech97.mdb"
      );
    return c;
    /*
     To use an already defined ODBC Datasource :    
     
        String URL = "jdbc:odbc:myDSN";
        Connection c = DriverManager.getConnection(URL, "user", "pwd"); 
        
    */     
    }
}

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]