How to create a JDBC program in Java

Introduction to JDBC

Before JDBC, we use drivers to establish connections between Java programs and databases such as mysql, Oracle and sqlserver. Drivers are what we will learn in this course. Then there is a problem: is the driver shared when establishing a connection between the same Java program and the database? If it's the same, you can obviously save code, but it's actually different. It must be noted that it is different. Then there is a problem: when the underlying database changes, the database driver will also change, so how to solve this problem? So JDBC appeared in order to reduce the workload of developers and improve the reuse of code.

JDBC API

JDBC structure

1. JDBC API is an integral part of java development kit (JDK):

2. Jdbc driver manager is the pillar of JDBC architecture. Its main function is to connect Java applications to the correct jdbc driver.

3. The jdbc driver test kit provides certain credibility for the operation of JDBC drivers. Only drivers that pass the jdbc driver test package are considered to comply with JDBC standards.

4. Jdbc-odbc bridge enables ODBC driver to be used as jdbc driver. Its goal is to facilitate access to some uncommon DBMS. Its implementation provides a way for the rapid development of JDBC.

Before we start, let's implement a simple JDBC program

step

1. Register driver; (only need to register once)

2. Establish connection;

3. Create an executed SQL statement;

4. Execute the statement;

5. Processing execution results;

6. Release resources;

Example

Analyze JDBC development steps

The basic steps for accessing a database using JDBC are generally as follows:

Develop the first JDBC program

Description of each statement

Load JDBC Driver

How to load the driver:

Where DriverName is the name of the jdbc driver to load. The driver name is determined according to the type of jdbc driver provided by the database manufacturer.

The method to load the MySQL database driver is:

The method of loading Oracle database driver is:

Create database connection - 1

Drivermanager class is the management layer of JDBC. It acts between users and drivers, tracks available drivers, and establishes a connection between the database and the corresponding drivers. This class is responsible for loading and registering JDBC drivers, and managing the connection between applications and registered drivers.

Common methods of drivermanager:

Create a database connection using getconnection() of the drivermanager class:

Create database connection - 2

URL provides a method to identify the location of the database, so that the corresponding driver can recognize the database and establish a connection with it.

The JDBC URL consists of three parts:

It can be changed according to the change of sub agreement.

It is a sub protocol, which refers to the way the database is connected.

Create database connection - 3

For MySQL driven links, the URL is:

Example:

Create SQL statement

After creating the connection, you can send SQL statements to the target database through this connection. Before sending SQL statements, you must create an object of statement class, which is responsible for sending SQL statements to the database. If the result set is generated after the SQL statement is run, the statement object will encapsulate the result set into a resultset object and return it.

Create a statement object by calling the createstatement method of the connection interface, such as:

Execute SQL statement

There are two very important methods in the statement interface: executeupdate method and executeQuery method. The executeupdate method can be used to execute SQL statements such as insert, modify and delete. The return value of the executeupdate method is an int type value, indicating the number of rows in the updated data table. The executeQuery method can be used to execute the SQL statement of database query operation. Its return value is the object implementing the resultset interface, in which the data queried from the database is saved.

For example:

There is no detailed description of the SQL command expansion. There are many comments in the code in the above example, which have detailed descriptions. There are also step instructions on how to write a JDBC program, one after another, including the declared object type. Another thing to pay attention to is when closing the program at last, It is also sequential. Statements with dependencies are closed in turn, which is also explained in the program code.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>