(2) JDBC is suitable for beginners from getting started to mastering!

Source: Internet
Author: User
Tags db2 driver
The document above describes how to establish a connection between a Java program and a database. Now we will discuss several JDBC drivers and their usage.

In fact, there are four types of JDBC drivers. Now I will introduce them one by one.
The first driver is called the JDBC-ODBC bridge.

This type of driver is attached to the"Open Database Connectivity"" (Open Database Connectivity) is what we often call ODBC drivers. To connect to a different data source, you only need to register with the ODBC Administrator or bind a different ODBC data source to the appropriate data source name.

Because the ODBC driver has a long time, it seems that it has been longer than Java, so there are many ODBC drivers. If you are a beginner, we recommend that you use this driver to connect your database to a Java program. Some ODBC drivers even allow you to specify ODBC data sources to Microsoft Excel applications or plain text files. However, when JDBC is converted to ODBC and then ODBC is converted to a specific database protocol, this extra intermediate level may cause performance degradation. Another potential problem with this driver is the use of distributed applications. Because the bridge itself does not support distributed communication, the only way this driver can run in the network is that the ODBC driver itself supports remote interaction. This is not a good choice for simple ODBC drivers, and although large databases do have ODBC drivers that can run remotely, they cannot be compared with pure Java JDBC drivers in terms of performance.

Let's take a look at the encoding method using this driver:

The JDBC-ODBC bridge driver class name is Sun. JDBC. ODBC. jdbcodbcdriver, jdbc url in the form of JDBC: ODBC: DSN, where DSN is the name of the data source that uses the ODBC Administrator to register the database. For example, if the Database Registration uses JDBC as the ODBC Data Source Name, Java as the user name, and sun as the password, the following code segment can be used to establish a connection.

For the sake of simplicity, this code saves the necessary error handling and check.
String url = "JDBC: ODBC: JDBC ";
Connection con;

Try{
Class. Forname("Sun. JDBC. ODBC. jdbcodbcdriver ");
}Catch(Java. Lang. classnotfoundexception e ){
System. Err. Print ("classnotfoundexception :");
System. Err. println (E. getmessage ());
Return;
}

Try{
Con = drivermanager. getconnection (URL, "Java", "Sun ");
}Catch(Sqlexception ex ){
System. Err. println ("sqlexception:" + ex. getmessage ());
}Finally{
Try{
Con. close;
}Catch(Sqlexception ex ){
System. Err. println (sqlexception: "+ ex. getmessage ());
}
}

The second driver is called part of the Java driver.

This is because it directly translates JDBC APIs into APIs of a specific database.

The database client application must have a suitable database client library that may contain installed binary code that is running. For distributed applications, this requirement may lead to additional license issues and may lead to terrible potential code distribution issues. For example, you can use this model to restrict developers to the client platforms and operating systems supported by the database vendor's client libraries.
However, this model works effectively when the client is strictly controlled. This usually occurs in the company's LAN. An example of this type of driver is the DB2 JDBC application driver. The following example shows how to use the DB2 driver to establish a connection.
String url = "JDBC: DB2: JDBC ";

Try{
Class.Forname("Com. IBM. db2.jdbc. App. db2driver ");
}Catch(Java. Lang. classnotfoundexception e ){
System. Err. Print ("classnotfoundexception :");
System. Err. println (E. getmessage ());
Return;
}
...
Note how similar the code segment is to the example of the previous model. This is the main selling point of this model: When programmers switch from one model to another, the learning curve is slight and almost does not exist.

Today, we will talk about the drivers of the other two types of databases. They are two pure Java drivers, the advantage of a pure Java driver is that it is easy to deploy in a distributed environment. What are their specific conditions?
I want to know what to do later, and listen to the next decomposition ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.