Home » Java » (4) Statement objects used by JDBC

(4) Statement objects used by JDBC

one Statement usage analysis

Statement objects are used to handle non parametric queries (plain queries) using the following steps:

1. creates Statement object

creates the Statement object.Eg:

through the createStatement () method of the Connection object

Statement st = conn.createStatement ();

2. sets the basic options

After you have created the Statement object, you can set the required property

by some method of the object.


Statement object setting method is as follows:



Statement st = conn.createStatement ();

St.setMaxRows (10); / / set the maximum number of rows returned here, set 10

3. performs query SQL


query can use the executeQuery () and execute () methods of the Statement object,.

The parameters of the two

methods are all a spliced String type string, SQL.


executeQuery () method can only execute a SQL and return the ResultSet object.


execute () method should only return multiple ResultSet objects in the statement,


when a combination of multiple update counts or ResultSet objects and update counts

can get ResultSet objects through getResultSet, and so on..

4. executeUpdate method


executeUpdate () method is used to execute the INSERT, UPDATE, or DELETE statements,

and SQLDDL (data definition language) statements, such as CREATE, TABLE, and DROP TABLE.INSERT, UPDATE

The effect of the

or DELETE statement is to modify a column or column in the zero or multiple rows of the table, which can be used to judge whether the execution succeeds or not, based on the result of the

The return value of

executeUpdate is an integer indicating the number of rows affected (that is, the update count).

for CREATE, TABLE, or DROP, TABLE, etc. do not operate rows of statements, the return value of executeUpdate is always zero,

so you can't think it's not successful because it returns 0.


// test executeUpdate

Int resultCount =

st.executeUpdate ("UPDATE, t_user_main, SET, userPwd ='testExecuteUpdate'",
"WHERE + oid ='1'");

5. closes the Statement object

calls the close () method of the Statement object,.

two Statement uses instances

1. creates the t_user_main table

CREATE, TABLE, `t_user_main`
`oid` varchar (32) NOT NULL COMMENT 'primary key',
`userName` varchar (255) DEFAULT NULL COMMENT 'user name',
`userPwd` varchar (255) DEFAULT NULL COMMENT 'login password',

2. insert data

INSERT, INTO, t_user_main (OID, userName, userPwd)
VALUES ('1','one','111111'), ('2','two','222222');

3. java code example

package com.lanhuigu.JavaBase.jdbc;
Import java.sql.Connection;
Import java.sql.DriverManager;
Import java.sql.ResultSet;
Import java.sql.Statement;
Public, class, JdbcStatementTest {
Public, static, void, main (String[], args) {
String user = "root"";
String password = "lanhuigu"";
* useUnicode=true: stands for using the Unicode character set
* characterEncoding=UTF8: character encoding
* does allowMultiQueries=true: allow batch processing?
* /
String url = "jdbc:mysql://, useUnicode=true?""
+ "&characterEncoding=UTF8&allowMultiQueries=true"";
String driver = "com.mysql.jdbc.Driver"";
//1. load driver
Class.forName (driver);
//2. establish connection
Connection conn = DriverManager.getConnection (URL, user, password);
//3. creates Statement objects
Statement st = conn.createStatement ();
St.setMaxRows (10); / / set the maximum number of rows returned here, setting 10
String SQL = "SELECT, oid, userName, userPwd, FROM, t_user_main."";
//4. query -- returns the query result
ResultSet rs = st.executeQuery (SQL);
//5. print query results
While (rs.next ()) {
System.out.println ("oid:", "+rs.getString" ("oid")
+ "userName:" + "rs.getString" ("userName")
+ "userPwd:" + "rs.getString" ("userPwd");
/ / test executeUpdate
Int resultCount =
St.executeUpdate ("UPDATE, t_user_main, SET, userPwd ='testExecuteUpdate'")"
"WHERE + oid ='1'"); / / from the database query, verify
System.out.println ("executeUpdateResult:" + resultCount);
//6. closes the connection
St.close (Statement); / / close object
(conn.close); / / close database connections
} catch (Exception, e) {
System.out.println (database connection failed);
console print:

With regard to the database effect after the execution of the executeUpdate () method, the userPwd of oid 1 was successfully updated,

also prints the update at the program console to 1, indicating that the update affects the number of rows to 1: