"Learning notes" JDBC Database connection technology (Java DB Connectivity)

Source: Internet
Author: User
Tags driver manager

I. Introduction of JDBC

Java is the use of JDBC technology to achieve access to various databases, JDBC is the Java database connection technology abbreviation. It can persist data and is a persistent mechanism.

1. Persistence

Persistence is the mechanism by which data in a program is transformed between the instantaneous state and the persistent state.

2.JDBC API

Connection Connection Interface

Statement interface

ResultSet result set interface

Perparedstatement interface

3.JDBC Driver Manager (Driver Manager)

Provided by Sun, it is the backbone of the JDBC architecture and is responsible for managing a variety of different drives (java.sql packages)

4.JDBC Drive

The JDBC driver is provided by various database vendors or third-party middleware vendors and is responsible for connecting various databases.

When developing Java applications, we only need to properly load the JDBC driver and properly call the JDBC API to make database access.

Second, the JDBC API

The 1.JDBC API does three things: establish a connection to the database, send SQL statements, and process the results.

DriverManager class: Loads the driver and provides support for creating a new database connection.

Connection class: Responsible for connecting to the database and acting as the data transfer task.

Statement class: Generated by the connection class, responsible for executing SQL statements.

ResultSet class: Responsible for saving and handling the results resulting from statement execution.

PreparedStatement class: Statement sub-interface, with high security, high performance, high readability and high maintainability of the advantages.

Third, JDBC access to the database steps

1. Load the JDBC Driver

Use the Class.forName () method to load a given JDBC driver class into a Java virtual machine. If a given class does not exist in the system, a ClassNotFoundException exception is thrown.

2. Establish a connection to the database

The DriverManager class is the management layer of JDBC, which acts between the user and the driver. Tracks the available drivers and establishes a connection between the database and the appropriate driver. When the getconnection () method is called, the DriverManager class first finds a driver from the list of loaded drivers that can receive the database URL, and then connects to the database with related data, creates the connection object, and returns the reference.

3. Send the SOL statement and get the return result

Once the connection is established, the object of the statement interface is created using the link and the statement is passed to the database to which it is connected: The query operation returns a result set of type resultset.

4. Handling return Results

Result set:

while (Rs.next ()) {

int id = rs.gitint ("id");

String name = rs.getstring ("name");

System.out.println (id+ "\ t" +name);

}

Four, two common driving modes

1.JDBC-ODBC Bridging mode (Driver class: "Sun.jdbc.odbc.JdbcOdbcDriver" database connection string starts with "JDBC:ODBC:" followed by the data source name)

Download ODBC driver

Configure an ODBC data source

2. Connect to a database using a pure Java approach

Common errors are in the following categories:

The name of the JDBC driver class is written incorrectly, and a classnotfoundexception exception occurs;

Database connection string, database user name, password write error, SqlException exception occurred;

After the database operation has ended, the database connection is not closed, resulting in system resources still being occupied.

The statement that closes the database connection is not placed in the finally statement, causing the statement to not be executed.

3. In the actual project development, in order to avoid the possible garbled problem, the establishment of the database connection encoding set for Utf-8,url connection:

URL = jdbc:mysql://126.0.0.1:3306/epet?useunicode=true&characterencoding=utf-8;

"Learning notes" JDBC Database connection technology (Java DB Connectivity)

Related Article

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.