#java.sql.Statement
# 描述
java.sql.Statement接口定义了向数据库发送SQL命令,以及从数据库获取结果集的方法与属性,通过Connection::createStatement方法创建。核心作用是:
- 执行DDL语句(如
CREATE TABLE、ALTER TABLE)、DML语句(如INSERT、UPDATE)、DQL语句(如SELECT)。 - 返回SQL执行结果(查询结果集
ResultSet、受影响行数)。 - 提供批量执行SQL、设置查询超时等辅助功能。
详细接口说明请参考JDBC官方网站 (opens new window)。
# 方法
YashanDB JDBC驱动对Statement接口有如下方法:
| 方法 | 返回类型 |
|---|---|
| executeQuery(String sql) | ResultSet |
| executeUpdate(String sql) | int |
| close() | void |
| setMaxFieldSize(int max) | void |
| getMaxFieldSize() | int |
| setMaxRows(int max) | void |
| getMaxRows() | int |
| execute(String sql) | boolean |
| addBatch( String sql ) | void |
| clearBatch() | void |
| executeBatch() | int[] |
| executeLargeBatch() | long[] |
| getResultSet() | ResultSet |
| getGeneratedKeys() | ResultSet |
| executeUpdate(String sql, int columnIndexes[]) | int |
| execute(String sql, int columnIndexes[]) | boolean |
| executeLargeUpdate(String sql, int columnIndexes[]); | long |
| executeUpdate(String sql, int autoGeneratedKeys) | int |
| execute(String sql, int autoGeneratedKeys) | boolean |
| executeLargeUpdate(String sql, int autoGeneratedKeys) | long |
| execute(String sql, String columnNames[]) | boolean |
| executeLargeUpdate(String sql, String columnNames[]) | long |
| executeUpdate(String sql, String columnNames[]) | int |
| getUpdateCount() | int |
| getResultSetConcurrency() | int |
| getResultSetType() | int |
| getConnection() | Connection |
| getResultSetHoldability() | int |
| isClosed() | boolean |
| setPoolable(boolean poolable) | void |
| isPoolable() | boolean |
| getLargeUpdateCount() throws SQLException | long |
| setLargeMaxRows(long max) throws SQLException | void |
| getLargeMaxRows() throws SQLException | long |
| executeLargeUpdate(String sql) throws SQLException | long |
| setFetchSize(int rows) | void |
| getFetchSize() | int |
| closeOnCompletion() | void |
| isCloseOnCompletion() | boolean |
| setQueryTimeout(int seconds) | void |
| getQueryTimeout() | int |
| cancel() | void |
| getMoreResults() | boolean |
| getMoreResults(int current) | boolean |
| setFetchDirection(int direction) | void |
| getFetchDirection() | int |
| setCursorName(String name) | void |
| setEscapeProcessing(boolean enable) | void |
| getWarnings() | SQLWarning |
| clearWarnings() | void |
# 示例
package quickstart;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class StatementExample {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
String url = "jdbc:yasdb://192.168.1.2:1688/yasdb";
Properties info = new Properties();
info.setProperty("user", "username");
info.setProperty("password", "passwd");
Class.forName("com.yashandb.jdbc.Driver");
return DriverManager.getConnection(url, info);
}
// 简单查询
public void simpleQuery(Connection conn) throws SQLException {
String sql = "SELECT id, name FROM users";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") +
", Name: " + rs.getString("name"));
}
}
}
// 执行DML
public void executeUpdate(Connection conn) throws SQLException {
String sql = "UPDATE users SET status = 'active' WHERE id = 1";
try (Statement stmt = conn.createStatement()) {
int rows = stmt.executeUpdate(sql);
System.out.println("Updated rows: " + rows);
}
}
// 执行多条语句
public void executeMultiple(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
// 执行INSERT
stmt.execute("INSERT INTO users (id, name, status) VALUES (102, 'Alice', 'inactive')");
// 执行UPDATE
int rows = stmt.executeUpdate("DELETE FROM users WHERE name = 'Bob'");
System.out.println("Deleted rows: " + rows);
// 执行SELECT
try (ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM users")) {
if (rs.next()) {
System.out.println("Total users: " + rs.getInt(1));
}
}
}
}
// 批量执行
public void batchExecution(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.addBatch("INSERT INTO users (id, name, status) VALUES (103, 'User1', 'inactive')");
stmt.addBatch("INSERT INTO users (id, name, status) VALUES (104, 'User2', 'inactive')");
stmt.addBatch("UPDATE users SET status = 'active' WHERE id > 100");
int[] results = stmt.executeBatch();
System.out.println("Batch executed successfully");
}
}
public static void main(String[] args) {
try (Connection conn = getConnection()) {
try (Statement stmt = conn.createStatement()) {
try {
stmt.execute("DROP TABLE users");
} catch (SQLException ignored) {
}
stmt.execute("CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(20), status VARCHAR(20))");
stmt.execute("INSERT INTO users VALUES (1, 'Alice', 'inactive')");
stmt.execute("INSERT INTO users VALUES (2, 'Bob', 'inactive')");
stmt.execute("INSERT INTO users VALUES (101, 'Carol', 'inactive')");
}
StatementExample example = new StatementExample();
example.simpleQuery(conn);
example.executeUpdate(conn);
example.executeMultiple(conn);
example.batchExecution(conn);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
正常情况下,程序输出如下:
ID: 1, Name: Alice
ID: 2, Name: Bob
ID: 101, Name: Carol
Updated rows: 1
Deleted rows: 1
Total users: 3
Batch executed successfully

