我是一个MySQL数据库管理员,但我对JDBC一无所知(除了“它与Java有关”,这足以让我觉得阅读相关内容很痛苦)......不过,看起来executeUpdate()
就是你要找的东西。
int executeUpdate(String sql)
throws SQLException
Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE
statement or an SQL statement that returns nothing, such as an SQL DDL statement.
最后一部分(“返回空的SQL语句”)听起来像是对SET @rank = 0
的贴切描述;就结果集而言,它返回了空值。
Parameters:
sql - an SQL Data Manipulation Language (DML) statement, such as INSERT,
UPDATE or DELETE; or an SQL statement that returns nothing,
such as a DDL statement.
Returns:
either (1) the row count for SQL Data Manipulation Language (DML)
statements or (2) 0 for SQL statements that return nothing
Throws:
SQLException - if a database access error occurs, this method is called on
a closed Statement or the given SQL statement produces a ResultSet object
我认为这与您用于“UPDATE usertwo...”查询的内容相同,因此,对同一数据库连接进行三个executeUpdate()调用,顺序执行即可实现您的意图。
或者,实际上,您只需要2次对数据库的调用,因为前两个可以合并为一个查询:
http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#executeUpdate%28java.lang.String%29
SET @rank = 0, @userCount = (select COUNT(*) from usertwo);
用户自定义变量在MySQL会话中持久存在,该会话绑定到与数据库的单个连接。