Detailed usage of Statement in java

Detailed usage of Statement in java

1. Create a Statement object

After a connection to a specific database is established, the connection can be used to send SQL statements. The Statement object is created with the method createStatement of Connection, as shown in the following code snippet:

Connection con = DriverManager.getConnection(url, “sunny”,"");
Statement stmt = con.createStatement();

In order to execute the Statement object, the SQL statement sent to the database will be provided as a parameter to the Statement method:

ResultSet rs = stmt.executeQuery(“SELECT a, b, c FROMTable2”);

2. Execute the statement using the Statement object

The Statement interface provides three methods for executing SQL statements: executeQuery, executeUpdate, and execute. Which method is used depends on the content generated by the SQL statement.

The executeQuery method is used to produce a single result set statement, such as a SELECT statement.

The executeUpdate method is used to execute INSERT, UPDATE or DELETE statements and SQLDDL (data definition language) statements, such as CREATE TABLE and DROP TABLE. The effect of an INSERT, UPDATE, or DELETE statement is to modify one or more columns in zero or more rows in the table. The return value of executeUpdate is an integer indicating the number of rows affected (that is, the update count). For statements that do not manipulate rows, such as CREATE TABLE or DROP TABLE, the return value of executeUpdate is always zero.

The execute method is used to execute statements that return multiple result sets, multiple update counts, or a combination of the two. Because most programmers will not need this advanced feature, it will be introduced in a separate section later in this overview.

All methods of executing the statement will close the currently open result set (if any) of the called Statement object. This means that before re-executing the Statement object, the processing of the current ResultSet object needs to be completed.

It should be noted that the PreparedStatement interface that inherits all the methods in the Statement interface has its own executeQuery, executeUpdate, and execute methods. The Statement object itself does not contain SQL statements, so you must provide SQL statements as parameters to the Statement.execute method. PreparedStatement objects do not provide SQL statements as parameters to these methods because they already contain precompiled SQL statements. CallableStatement objects inherit the PreparedStatement form of these methods. For PreparedStatement or CallableStatement versions of these methods, using query parameters will throw SQLException.

3. The statement is complete

When the connection is in auto-commit mode, the executed statement will be automatically submitted or restored when it is completed. The statement is considered completed when it has been executed and all results are returned. For the executeQuery method that returns a result set, the statement is completed when all rows of the ResultSet object are retrieved. For the method executeUpdate, the statement is complete when it executes. But in a few cases where the execute method is called, the statement is not completed until all the result sets or the update count it generates are retrieved.

statement-related overview

The Statement object is used to send SQL statements to the database. There are actually three kinds of Statement objects, all of which serve as containers for executing SQL statements on a given connection: Statement, PreparedStatement (which inherits from Statement) and CallableStatement (which inherits from PreparedStatement). They are all dedicated to sending specific types of SQL statements: Statement objects are used to execute simple SQL statements without parameters; PreparedStatement objects are used to execute pre-compiled SQL statements with or without IN parameters; CallableStatement objects are used to execute data stored in the database The call of the procedure.

The Statement interface provides basic methods for executing statements and obtaining results. The PreparedStatement interface adds methods for processing IN parameters; and CallableStatement adds methods for processing OUT parameters.

Some DBMS treat each statement in a stored procedure as a separate statement; others treat the entire procedure as a compound statement. This difference becomes very important when auto-commit is enabled, because it affects when the commit method is called. In the former case, each statement is submitted separately; in the latter case, all statements are submitted simultaneously.

4. Close the Statement object

The Statement object will be automatically closed by the Java garbage collector. As a good programming style, you should explicitly close Statement objects when they are not needed. This will release DBMS resources immediately and help avoid potential memory problems.

Guess you like

Origin blog.csdn.net/weixin_43088443/article/details/112999909