Mishthi
Mishthi

Reputation: 1161

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

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?

Upvotes: 11

Views: 22490

Answers (6)

rustyx
rustyx

Reputation: 85266

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.

Upvotes: 5

Dave
Dave

Reputation: 7025

My understanding is that you would not want to - it would become tedious and error prone when things dont go perfectly.

I.E. you can't catch an exception when/if you invoke a non java DLL from inside java.

Upvotes: 0

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.

Upvotes: 1

Berin Loritsch
Berin Loritsch

Reputation: 11463

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.

Upvotes: 1

systempuntoout
systempuntoout

Reputation: 74064

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

Upvotes: 1

Boris Pavlović
Boris Pavlović

Reputation: 64622

You can use JDBC-ODBC drivers

Upvotes: 0

Related Questions