[英]Get status message of MySQL query (including execution time) with JDBC
Suppose that I am implementing a query in MySQL say 假设我在MySQL中实现查询说
Create Database newdb;
then the database responds the result 然后数据库响应结果
database created in 0.05 sec
Now my question is how to get this message in a Java program using JDBC? 现在我的问题是如何使用JDBC在Java程序中获取此消息?
The following code works for me 以下代码对我有用
try (Statement s = conn.createStatement()) {
s.execute("SET PROFILING=1;");
s.execute("CREATE DATABASE newdb;");
try (ResultSet rs = s.executeQuery("SHOW PROFILES;")) {
rs.next();
System.out.printf(
" Statement: %s%nExecution time: %f seconds.%n",
rs.getString("Query"),
rs.getDouble("Duration"));
}
s.execute("SET PROFILING=0;");
}
The console output is 控制台输出为
Statement: CREATE DATABASE newdb
Execution time: 0.002014 seconds.
No you cannot do that. 不,你不能那样做。 Not at least with JDBC. 至少不是JDBC。 All you can do is - 您所能做的就是-
Class.forName(jdbcDriver);
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=");
Statement statement = connection .createStatement();
int result = statement.executeUpdate("CREATE DATABASE IF NOT EXISTS" + dbName)
and you will get result
as 0. 您将得到result
为0。
Note that when the return value for executeUpdate is 0, it can mean one of two things: 请注意,当executeUpdate的返回值为0时,它可能意味着以下两种情况之一:
The statement executed was an update statement that affected zero rows. 执行的语句是影响零行的更新语句。
The statement executed was a DDL statement. 执行的语句是DDL语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.