Java JDBC connection database code and steps detailed and example code

Detailed explanation of JDBC connection database code and steps in Java

JDBC connection database

• create a program to connect 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 name of the bridge connection driver or database management system.

Data source ID: mark the address and connection port where the database source is found.

For example: (connection URL of MySQL)

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

Password.

For example:

4. Create a statement

• to execute SQL statements, 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 SQL statements 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 and return multiple result sets A statement that contains multiple update counts or a combination of both.

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

Thank you for reading, hope to help you, thank you for your support to this site!

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