JDBC (1)

Source: Internet
Author: User
Tags driver manager
Persistence saves data to a power-down storage device for later use. In most cases, especially for enterprise-level applications, Data Persistence means that the data in the memory is saved to the hard disk for "solidification", while the persistence implementation process is mostly completed through various relational databases. The main application of persistence is to store data in the memory

Persistence saves data to a power-down storage device for later use. In most cases, especially for enterprise-level applications, Data Persistence means that the data in the memory is saved to the hard disk for "solidification", while the persistence implementation process is mostly completed through various relational databases. The main application of persistence is to store data in the memory


Persistence)

Save the data to a power-down storage device for later use. In most cases, especially for enterprise-level applications, Data Persistence means that the data in the memory is saved to the hard disk for "solidification", while the persistence implementation process is mostly completed through various relational databases.

Persistence is mainly used to store data in memory in relational databases. In addition, it can also be stored in Disk Files and XML data files.

In Java, database access technology can be divided into the following types:

> Direct JDBC access to the database

> JDO Technology

> Third-party O/R tools, such as Hibernate and ibatis

JDBC is the cornerstone for java to access the database. JDO and Hibernate only better encapsulate JDBC.

JDBC (Java Database Connectivity)

It is a public interface (a set of Apis) independent of a specific database management system and universal SQL database access and operations. It defines a standard Java class library used to access the database, this class library provides a standard method for convenient access to database resources.

JDBC provides a unified way to access different databases and shields developers from some details.

The goal of JDBC is to allow Java programmers to use JDBC to connect to any database system that provides the JDBC driver. In this way, programmers do not need to have too much knowledge about the characteristics of specific database systems, this greatly simplifies and accelerates the development process.
The following figure shows the differences between Java application development without JDBC participation.







JDBC Architecture

The JDBC interface (API) has two levels:

1. application-oriented APIs: Java APIs, abstract interfaces, and supplied to application developers (connecting to databases, executing SQL statements, and obtaining results ).
2. Database-oriented API: Java Driver API for developers to develop database drivers.



JDBC driver category

JDBC driver: class libraries of JDBC implementation classes created by database vendors according to JDBC specifications.

There are four types of JDBC drivers:

Category 1: JDBC-ODBC bridge.
Class 2: some local APIs and some Java drivers.
Category 3: JDBC network pure Java driver.
Class 4: pure Java driver for local protocols.

The third and fourth types are pure Java drivers. Therefore, Java developers have advantages in performance, portability, and functions.

ODBC

Early access to the database called the proprietary API provided by the database vendor. To provide a unified access method on the Windows platform, Microsoft released ODBC (Open Database Connectivity and Open Database connection) and provided ODBC APIs, the user only needs to call the odbc api in the program. The ODBC driver converts the call into a call request to a specific database.

An ODBC-based application does not rely on any DBMS (database manager system) for database operations, and does not directly deal with DBMS. All database operations are completed by the corresponding dbms odbc driver. That is to say, both FoxPro, Access, MYSQL, and Oracle databases can be accessed using ODBC APIs. It can be seen that the biggest advantage of ODBC is that it can process all databases in a unified manner.

JDBC-ODBC Bridge

The JDBC-ODBC Bridge itself is also a driver, which can be used to access the database through ODBC JDBC-API. In fact, this mechanism converts standard JDBC calls to corresponding ODBC calls and accesses the database through ODBC.

Because it needs to be called through multiple layers, it is less efficient to access the database by using the JDBC-ODBC bridge.

In JDK, the implementation class of the JDBC-ODBC bridge is provided (sun. jdbc. odbc. JdbcOdbcDriver ).



Some local APIs and some Java drivers

This type of JDBC driver is written in Java, which calls the local API provided by the database vendor.

Using this type of JDBC driver to access the database reduces the ODBC call Link and improves the database access efficiency.

In this way, you need to install the local JDBC driver and the local API of a specific vendor on the customer's machine.



JDBC network pure Java driver

This driver uses the middleware application server to access the database. The application server acts as a gateway to multiple databases, through which clients can connect to different database servers.

The application server usually has its own network protocol. The Java User program sends a JDBC call to the application server through the JDBC driver, and the application server uses a local program driver to access the database to complete the request.



Pure Java driver for local Protocol

Most database vendors have supported network protocols that allow customers' programs to communicate directly with databases over the network.

This type of driver is fully written in Java. Through the Socket connection established with the database, the JDBC call is converted into a directly connected network call using the network protocol specific to the vendor.



JDBC API

JDBC APIs are a series of interfaces that allow applications to connect to databases, execute SQL statements, and obtain returned results.



Driver Interface

The Java. SQL. Driver Interface is required by all JDBC drivers. This interface is provided to database vendors, and different database vendors provide different implementations.

In a program, you do not need to directly access the classes that implement the Driver interface. Instead, the Driver manager class (java. SQL. DriverManager) calls these Driver implementations.

Load and register the JDBC driver

To load a JDBC driver, call the static method forName () of the Class and pass the name of the JDBC driver to be loaded to it.

The DriverManager class is a driver manager class that manages drivers.

Generally, you do not need to explicitly call the registerDriver () method of the DriverManager class to register an instance of the Driver class. Because the Driver class of the Driver interface contains a static code block, in this static code block, will call DriverManager. the registerDriver () method to register an instance.

Establish a connection

You can call the getConnection () method of the DriverManager class to establish a connection to the database.

The jdbc url is used to identify a registered driver. The driver manager selects the correct driver through this URL to establish a connection to the database.

The jdbc url standard consists of three parts, which are separated by colons.

Jdbc: <子协议> : <子名称>

1. Protocol: the protocol in the jdbc url is always jdbc;
2. Sub-Protocol: the sub-protocol is used to identify a database driver;
3. Sub-Name: a method to identify a database. Sub-names can be changed according to different sub-protocols. The sub-names are used to locate the database and provide sufficient information.

JDBC URLs of common databases



Access Database

A database connection is used to send commands and SQL statements to the database server. After the connection is established, you need to access the database and execute SQL statements.

In the java. SQL package, three interfaces define different methods for calling databases:











Jdbc api Summary

> Java. SQL. DriverManager is used to load drivers and obtain database connections.

> Java. SQL. Connection completes the Connection to a specified database

> Java. SQL. Statement serves as the container for SQL Statement execution declaration in a given connection. It contains two important subtypes.

> Java. SQL. PreparedSatement is used to execute the pre-compiled SQL statement.

> Java. SQL. CallableStatement is used to call stored procedures in databases.

> Java. SQL. ResultSet

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.