Summary of database connection based on JDBC in java development

This paper describes the method of connecting database based on JDBC in java development. Share with you for your reference, as follows:

Create a program that connects to the database with JDBC, including 7 steps:

1. Load JDBC Driver:

Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java virtual machine), which is implemented through the static method forname (string classname) of the java.lang.class class class.

For example:

After loading successfully, an instance of the driver class will be registered in the drivermanager class.

2. Provide the URL of the JDBC connection

The connection URL defines the protocol, sub protocol and data source ID when connecting to the database.

Writing form: protocol: sub protocol: data source identification protocol: always start with JDBC in JDBC. Sub protocol: is the driver of bridge connection or the name of database management system. Data source ID: mark the address and connection port where the database source is found.

For example: (connection URL of MySQL)

jdbc:MysqL: //localhost:3306/test? useUnicode=true&characterEncoding=gbk ;

Useunicode = true: indicates that the Unicode character set is used. If characterencoding is set to GB2312 or GBK, this parameter must be set to true. Characterencoding = GBK: character encoding method.

3. Create a connection to the database

To connect to the database, you need to apply to Java sql. The drivermanager requests and obtains a connection object, which represents a connection to a database.

Use the getconnect in (string URL, string username, string password) method of drivermanager to pass in the path of the specified database to be connected, the user name and password of the database.

For example:

4. Create a statement

To execute an SQL statement, you must obtain Java sql. Statement instance, which is divided into the following three types:

1. Execute static SQL statements. It is usually implemented through a statement instance. 2. Execute dynamic SQL statements. It is usually implemented through the Preparedstatement instance. 3. Execute database stored procedures. It is usually implemented through a callablestatement instance.

Specific implementation method:

5. Execute SQL statement

The statement interface provides three methods to execute SQL statements: executeQuery, executeupdate, and execute

1. Resultset executeQuery (string sqlstring): execute the SQL statement to query the database and return a resultset object.

2. Int executeupdate (string sqlstring): used to execute insert, update or delete statements and SQL DDL statements, such as create table and drop table

3. Execute (sqlstring): used to execute statements that return multiple result sets, multiple update counts, or a combination of the two.

Specific implementation code:

6. Processing results

Two cases:

1. Executing the update returns the number of records affected by this operation. 2. The result returned by executing the query is a resultset object.

The resultset contains all rows that meet the conditions in the SQL statement, and it provides access to the data in these rows through a set of get methods.

Get data using the access method of the resultset object:

(columns are numbered from left to right and start with column 1)

7. Close JDBC object

After the operation is completed, close all used JDBC objects to release JDBC resources. The closing order is opposite to the declaration order:

1. Close recordset 2. Close declaration 3. Close connection object

Supplement: JDBC connection to Oracle database example

I hope this article will be helpful to you in Java programming.

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
分享
二维码
< <上一篇
下一篇>>