简体   繁体   中英

About JDBC drivers

I read about Drivers that uses JDBC API in a book. Not clear about the explanation. I want to know what type of driver does the below code uses. Is it type 4 driver( java driver provided by the database vendor). It was mentioned nothing needs to be installed in the client application for type 4 drivers. But still we need to have the class file com.mysql.jdbc.Driver for the code to work. Not sure what is meant here.

Also it was mentioned type 2 drives use the binary code of the driver by the database vendor and it needs to be installed in the client. How it differs from the below example. Would be helpful if a sample code can be pasted for accessing type 2 drivers.

Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/coffeebreak";
conn = DriverManager.getConnection(url, "username", "password");
doTests();
conn.close();

When you see the phrase "nothing needs to be installed" in reference to type 4 JDBC drivers (especially in old documentation), it refers to the fact that nothing besides the driver jar itself needs to be installed on the client machine. You will need the driver jar no matter what, but with the other types:

  • Type 1: ODBC and an ODBC driver will need to be installed on the client system
  • Type 2: a native database driver will need to be installed on the client system
  • Type 3: a protocol proxy server will need to be installed between the client system and the database

So for example, an Oracle type 2 driver (in addition to the jdbc jar itself), requires a complete oracle client installation on the machine that will run the java code.

A couple of notes:

  • Since java 6 (IIRC), with a modern jdbc driver, you don't need the Class.forName statement any more. Newer JDBC drivers now register themselves automatically.

  • You can't tell just by looking at the code that uses it (that's the whole point), but the mysql jdbc driver is a type 4 driver.

  • As you can't tell the difference by using the driver, the sample code to use a type 2 driver is the same as what you've provided in the question.

  • All types of jdbc driver (type 1 excepted, as there was only 1 instance of that) feature drivers provided by the vendor. The type refers to how the driver connects to the database, not who provides it.

  • Type 2 drivers are pretty rare these days, and IMO, nobody really uses type 1 or 3 drivers anymore at all.

  • These days, almost all modern, production-worthy jdbc drivers are type 4. (I know that oracle provides both a type 2 and a type 4 driver, but that's the only one I can think of, and the two drivers have the same features.) Use type 4 unless you have some highly specific advice from the database vendor.

Also, the language you are quoting sounds pretty dated. The basics of JDBC haven't significantly changed in a while, but even so you might want to check out a newer book.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM