Reputation: 481
Suppose that I am implementing a query in MySQL say
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?
Upvotes: 2
Views: 3480
Reputation: 123409
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.
Upvotes: 5
Reputation: 68905
No you cannot do that. Not at least with 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.
Note that when the return value for executeUpdate is 0, it can mean one of two things:
The statement executed was an update statement that affected zero rows.
The statement executed was a DDL statement.
Upvotes: 1