Can we use odbc only with java to connect to databases?

15,853

Solution 1

Sun JRE contains a built-in JDBC/ODBC driver (sun.jdbc.odbc.JdbcOdbcDriver). Here's an example how to use it: http://www.javacoffeebreak.com/articles/jdbc/

The driver was removed in Oracle JRE 8, so use Java version 7 or earlier.

Solution 2

You can't use ODBC directly because your JAVA program needs to use the JDBC driver to interact with the Database.

Solution 3

As others have mentioned you can use the JDBC/ODBC bridge driver. (Repeating @Rustam's link here: http://www.javacoffeebreak.com/articles/jdbc/).

There are a couple things to keep in mind when using the JDBC-ODBC bridge. First: it's use was not recommended by Sun for various reasons. The top three implications of using the bridge instead of a proper JDBC driver are:

  • Not every feature of JDBC is supported. ODBC is a more restrictive API, so some features (like savepoints in transactions) are not supported. However, the most common features like prepared statements are.
  • The native code to Java runtime translation is much slower than if you were doing everything in Java.
  • The JDBC/ODBC driver is more fragile than the appropriate JDBC driver. Essentially, if implementers of the ODBC driver don't do things a certain way, the JDBC driver will fail and throw some extra exceptions you might not be able to catch. In particular, you will be more susceptible to memory leaks. If you aren't building a long running service you might be OK.

That said, the JDBC/ODBC driver will work for a database that does not have direct JDBC support (most major databases do). Sometimes you don't need all those fancy features and just want to throw something together quickly. The JDBC/ODBC driver is designed for that.

Solution 4

Short answer : NO.

ODBC ( Open Database Connectivity ) hides the details of what database you are talking to. It has nothing to do with Java. If java programs need to talk to the database, then they have to interact with ODBC drivers. To interact with ODBC drivers, you need JDBC-ODBC drivers which hides the details of how the communication happens. You can pretty much make a few method calls and all set to go. The power of abstraction.

Share:
15,853
Mishthi
Author by

Mishthi

Updated on June 06, 2022

Comments

  • Mishthi
    Mishthi almost 2 years

    Do we always have to use jdbc with Java programs for making connectivity with database or can we use only odbc for connecting to databases with Java programs?

  • Mishthi
    Mishthi over 13 years
    No I know how to use jdbc-odbc.. I just want to know that can't I directly use odbc without using jdbc driver???
  • rustyx
    rustyx over 13 years
    You can use ODBC directly from a C application, because ODBC is a DLL in Windows. You can't invoke DLLs from Java directly, you need some kind of JNI bridge, that's what JdbcOdbcDriver is basically.
  • Mishthi
    Mishthi over 13 years
    ok thanx... and do we have to use jdbc-odbc driver cant i simply use jdbc for interacting with databases no need to use odbc ??
  • Mishthi
    Mishthi over 13 years
    Do I always need odbc for accessing database cant I only use jdbc for interacting with databases only
  • rustyx
    rustyx over 13 years
    Yes, you can just use jdbc from java, if your database has a pure-java driver. You'll have to include this driver (a JAR file) in the classpath. For example for Oracle it is ojdbc14.jar.
  • Mishthi
    Mishthi over 13 years
    Correct me if I am wrong... We have to use Jdbc always at java end for connecting to database... However we may not use odbc in case database have jdbc compatibility....and if databases dont support jdbc then we have to use odbc... This is what I have understood..Is it correct??
  • Gord Thompson
    Gord Thompson about 9 years
    2015 update: The JDBC-ODBC Bridge is obsolete and has been removed from Java 8.
  • Gord Thompson
    Gord Thompson about 9 years
    2015 update: The JDBC-ODBC Bridge is obsolete and has been removed from Java 8.