Statement of JDBC Interface Introduction

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

Statement:

1. Introduction: A series of operations on database data depend on SQL statements, and the Statement interface is used to execute SQL statements. The Statement object needs to be created through the createStatement() method in the Connection class. As shown below:

First create a class that connects to the database, and then you don't need to write this piece of code, you can directly call the methods in this class:

image

Create a Statement object:

image

2. The method of executing the SQL statement:

SQL statements can be roughly divided into two categories: DML (update statement) and DQL (query statement). Statements have different methods to operate these two types of statements.

2.1 int executeUpdate ("SQL statement"): This method is used to execute update statements, such as create, modify, delete, etc. The return value of this method is int type, which reflects the number of rows affected. The use case is as follows:

image

This can be verified in the SQLyog tool:

image

Note: This code can only be run once and cannot be run repeatedly, otherwise an error will be reported, because after running it once, there will be a mydb database, and running it again will create another mydb database with a duplicate name.

2.2 ResultSet

executeQuary ("SQL statement"): This method is used to execute a query statement. The type of its return value is a ResultSet interface. The reason why an array or collection is not used as the return value type, but ResultSet is used as the return value type This is because if you use an array or a collection, it means that the data needs to be queried at one time. If the query data is small, it is fine, but if the query data is extremely large, it is not conducive to the optimization of the database. ResultSet is an interface, and the specific implementation code is determined by the database developer. The database developer can decide whether it is a one-by-one query or a one-time query, which leaves room for the database developer and is conducive to program optimization. The use case is as follows:

image

For details on how to output the content of the query, please refer to the notes on ResultSet and ResultSetMetaData.

3. Batch SQL execution:

There is a design of batch SQL execution in java, that is, batch execution of SQL statements. The original intention of its design is to accumulate the SQL statements we wrote in the program first, and then execute multiple SQL statements at one time, but in fact Whether it is a one-time execution or a sentence by sentence execution depends on the driver class written by the database developer, and we don't need to care about these small details, because this is not what we care about, we only need to know and use This operation can be.

3.1 addBatch() accumulates SQL statements

3.2 executeBatch()

Execute SQL statements in batches, the type of the return value is an array type, and the number of rows affected by each SQL statement is returned.

3.3 clearBatch() clears batch SQL statements

3.4 Code implementation (take insertion as an example below):

image

The SQLyog verification results are as follows:

image

4. Tips: When writing SQL statements, it is best to write them in SQLyog to see if they are correct and then copy them to the program, because if an error is reported in the program because the SQL statement is written incorrectly, it will not prompt what it is Wrong, the content of the error is the same as the content of the error in the database.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944342&siteId=291194637