JDBC ODBC Differences

Source: Internet
Author: User
Tags driver manager odbc odbc connection stmt access database

One. JDBC (Java DataBase Connectivity Standard)

1.JDBC, which is an object-oriented application interface (API) that allows access to a variety of relational databases.

2. Driver (JDBC driver)

To access a particular database through JDBC, you must have the appropriate JDBC driver, which is often provided by the manufacturer of the production database and is the bridge between the JDBC API and the specific database.

3.DBMS

4. Linkages between the three

The JDBC API calls the driver to access the DBMS

Two. ODBC (Open database Connectivity, Internet connection)

ODBC is a set of specifications established by Microsoft and provides a set of standard APIs for database access.

1. ODBC Administrator (Administrator)

The main task of the program is to manage installed ODBC drivers and manage data sources.

2. Driver Manager (Driver Manager)

The task of the driver manager is to manage the ODBC driver, which is the most important part of ODBC.

3. ODBC Driver

is provided by Microsoft, which provides an interface between ODBC and the database.

4. Data sources

Data sources contain information such as database locations and database types, drivers, and so on, which is actually an abstraction of a data connection.

5. The relationship between the components

To access a database, the application must first register a data source with the ODBC Administrator, and the manager establishes the ODBC connection to the specific database based on the data source.

In ODBC, the ODBC API does not have direct access to the database and must exchange information with the database through the driver manager. The driver manager is responsible for passing the application's call to the ODBC API to the correct driver, and the driver returns the results to the application through the driver manager after performing the appropriate operation.

Three. How JDBC connects the DBMS 1.JDBC connection DBMS

(1) Import Package:

This requires that you have a package that contains the JDBC classes required for database programming. In most cases, using import java.sql.* is sufficient, as follows:

STEP 1. Import Required Packages
Import java.sql.*;

(2) Load the JDBC driver:

This requires the initialization of the driver so that the communication channel with the database can be opened. The following are the code snippets to achieve this goal:

STEP 2:register JDBC Driver
Class.forName ("Com.mysql.jdbc.Driver");

(3) Establish the connection:

This requires using the Drivermanager.getconnection () method to create a connection object that represents a physically connected database, as follows:

STEP 3:open a connection
Database credentials
Static final String USER = "username";
Static final String PASS = "password";
SYSTEM.OUT.PRINTLN ("Connecting to Database ...");
conn = Drivermanager.getconnection (Db_url,user,pass);

(4) Execute a query:

This requires building with an object type of statement or PreparedStatement and committing an SQL statement to the database. As follows:

STEP 4:execute a query
SYSTEM.OUT.PRINTLN ("Creating statement ...");
stmt = Conn.createstatement ();
String SQL;
sql = "SELECT ID, First, last, age from Employees";
ResultSet rs = stmt.executequery (SQL);

If you have a SQL Update,insert or DELETE statement, you need the following code snippet:

STEP 4:execute a query
SYSTEM.OUT.PRINTLN ("Creating statement ...");
stmt = Conn.createstatement ();
String SQL;
sql = "DELETE from Employees";
ResultSet rs = stmt.executeupdate (SQL);

(5) Extracting data from the result set:

This step is required in case the data is fetched from the database. The data results that can be retrieved using the appropriate resultset.getxxx () method are as follows:

STEP 5:extract data from result set
while (Rs.next ()) {
Retrieve By column name
int id = rs.getint ("id");
int age = Rs.getint ("Age");
String first = rs.getstring ("First");
String last = rs.getstring ("last");

Display values
System.out.print ("ID:" + ID);
System.out.print (", Age:" + age);
System.out.print (", First:" + first);
System.out.println (", Last:" + last);
}

(6) Clean up the environment:

All database resources should be explicitly closed, and the JVM-dependent garbage collection is as follows:

STEP 6:clean-up Environment
Rs.close ();
Stmt.close ();
Conn.close ();

2.JDBC-ODBC Bridge Connection

(1) Create a data source

(2) Load driver

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

(3) Establishing a connection

Connection con = drivermanager.getconnection (URL, "MyLogin", "MyPassword");

The JDBC URL begins with Jdbc:odbc: The remaining URLs are usually your data source name or database system.

(4) Same as JDBC Mode (4) (5) (6).

Four. The difference between JDBC and ODBC

1. JDBC is the way in which Java accesses the URL of DB over the network, and ODBC is the way in which connections are established locally.

Or: The former is the official version of the database, the latter is a generic version of the specification.

2. Different drivers: The JDBC driver is provided by the database vendor, and the ODBC driver is provided by Microsoft.

3. JDBC or JDBC-ODBC Bridge connections are generally used in Java, and Java does not directly invoke the ODBC API because ODBC is written in C and lacks portability and security.

4. Jdbc-odbc Bridge connection is generally used for local learning or LAN, while the JDBC mode can be used for cross-platform porting and wide application.

5. Jdbc-odbc Bridge connection is generally used before JDK 5. Because the JDBC driver was not provided by the server vendor prior to JDK 5, only the ODBC Bridge connection could be used.

Five. Access database Connection Example

  There should be only jdbc-odbc bridging when connecting to an Access database, no JDBC mode.

Import java.sql.*;

public class Dbconn {
Static final String dbdriver = "Sun.jdbc.odbc.JdbcOdbcDriver";
Static final String strconn = "Jdbc:odbc:driver={microsoft Access driver (*.mdb)};D bq=c:/apache-tomcat-6.0.45/webapps/ Site/db/datadb.mdb ";
Static final String dbusername= "";
Static final String dbpassword= "";
Connection Conn=null;
ResultSet Rs=null;
public static String Dbconn () {
String SERR = "";
try {
Java.sql.DriverManager.registerDriver ((Java.sql.Driver) (Class.forName (Dbdriver). newinstance ()));
}catch (Exception e) {
SERR = E.tostring ();
}
return (SERR);
}

Java.sql.Connection cn () throws SQLException {
Return Java.sql.DriverManager.getConnection (strconn, Dbusername, Dbpassword);
}

}

JDBC ODBC Differences

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.