The comp.lang.java.databases FAQ

Version 1998-05-24-01

1. Intro

    1.1 What does this FAQ cover?
    1.2 What if I have questions which aren't covered here?
2 JDBC
    2.1 What is JDBC?
    2.2 What driver do I need?
    2.3 How do I set up my driver?
    2.4 How do I figure out what JDBC URL I need to use?
    2.5 What do I have to do to make my first connection from Java?
3. SQLJ
4. Links
5. About this FAQ


1. Intro

1.1 What does this FAQ cover?

Anything related to both Java and databases. This includes connectivity from Java programs to SQL databases, Java stored procedures running inside a database server, OORDBMS's written in 100% pure Java that run in the same VM as the application - whatever.

Note that for the time being, it is pretty much a "how to get JDBC working" FAQ, but hopefully as time goes on will grow in scope.
 

1.2 What if I have questions which aren't covered here?

Post them to the newsgroup. Don't e-mail me. I won't answer direct e-mail questions. I will only answer newsgroup posts, if I know the answer or have a good guess. Other people may know the answer better than me, or they may just have free time sooner than I do. Share your questions and answers, use the usenet for what it's best suited to do. It will pay off for you.

(I have only been using JDBC for a few months and am not the world's premier expert on Java and databases so if it's not in this FAQ I probably only have a guess as to what the answer would be anyway.)


-= now for the good stuff =-
 

2. JDBC

2.1 What is JDBC?

This is answered pretty well at <http://java.sun.com/products/jdk/1.1/docs/guide/jdbc/getstart/intro.doc.html>.

Or, from <http://java.sun.com/products/jdk/1.1/docs/guide/jdbc/index.html>:

       Java Database Connectivity is a standard SQL database access interface, providing uniform access to a wide range of
       relational databases. It also provides a common base on which higher level tools and interfaces can be built. This comes
       with an "ODBC Bridge". The Bridge is a library which implements JDBC in terms of the ODBC standard C API.

Another way of saying this is, JDBC specifies an API for database drivers, and for your programs so they can use those database drivers. This means that for 5 different database products, if you can find a JDBC driver for each one, all of them work the same way, at least as far as making connections and executing queries and reading results, even though the SQL syntax you use for each given database may differ a bit.

This is a good thing for many reasons. Mainly, Java is all about interoperability, so the more you can insulate your Java programs (and your own expertise) from being tied to one vendor and one database, the better. Specficially:

2.2 What driver do I need?

"It depends." Mainly it depends on which database you have, but if you have one of the popular SQL databases (Oracle, Informix, Sybase SQL Server, MS SQL Server, IBM DB2, etc.) you will find that there are a variety of drivers available. So how do you pick one?
 
 Well, the other part of "it depends" is specific to the implementation details of the driver. This is not a programming issue, it's an integration issue - how do you want the underlying, vendor-specific database connectivity to be implemented? Ideally you could just tell a sysadmin "set me up with a JDBC driver, I don't care how it works as long as it works" but in reality the JDBC programmer is also the person who has to get the connectivity working too.

There are four types of JDBC drivers, distinguished by how much their implementation depends on platform-native "glue". This distinction is described well at <http://java.sun.com/products/jdk/1.1/docs/guide/jdbc/getstart/intro.doc.html#1005870>.

The distinction is now clear between the four types, but which one do you pick? In most cases, shoot for a Type 4 driver. All you do is add a .zip or .jar file to your classpath and you're done. (Sometimes the vendor adds some license management stuff but that's just another .class file.)

Some have argued that a Type 2 driver is the right choice because it is faster (because of its native implementation), and this may be true. However, in my experience the real-time overhead of a database query has very little to do with the driver and much more to do with things like establishing a TCP/IP connection and authenticating with the database, etc. so it shouldn't really matter unless you expect to be getting 10,000 row result sets back. In that case I suggest that there is probably something seriously wrong with your architecture and you might want to re-think it. If you are writing a bulk importer/exporter in Java, be my guest, use a Type 2 driver.

Type 3 usually involves a commercial bridge product running on the database server which uses ODBC or a platform-native driver to connect to the database. Performance usually suffers due to the added tier, and although multitier architecture is usually a good thing, in this case you can't add any intelligence to the added tier, so it's more of a necessary evil. Type 3 drivers are the best choice for distributed Java applications (applets, or applications running in a VM on a different machine) when you can't find a Type 4 driver.

2.3 How do I set up my driver?

 

2.4 How do I figure out what JDBC URL I need to use?

 

2.5 What do I have to do to make my first connection from Java?


3. SQLJ


4. Links

A list of JDBC drivers for many databases can be found here: http://ourworld.compuserve.com/homepages/Ken_North/jdbcvend.htm


5. About this FAQ

I hope to learn more about Java database programming as a result of having a FAQ. Nobody else was maintaining one so I decided to. If you can do a better job be my guest, just as long as there's a FAQ and it's at least as good as this one. I am but a humble programmer who has successfully gotten JDBC working, with help. I "get it" now and I'm sharing the knowledge I have with others so everybody can benefit, including me.

Read the documentation if you really want to learn what the precise specs are. I highly recommend Sun's own documentation and tutorials, and O'Reilly books, particularly Java in a Nutshell. They have helped me tremendously.

Jamie Flournoy (jamie@westlake.com)