I want to creata a database in mysql using a code snippet similar to the following using java
Statement statement = connection.createStatement();
String query = String.format(C"REATE DATABASE %s" , database);
statement.executeUpdate(query);
But spotbugs gives passes a nonconstant String to an execute or addBatch method on an SQL statement
error. When tried to create a prepared statement using the query
, it give the error A prepared statement is generated from a nonconstant String
.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("CREATE DATABASE ");
stringBuilder.append(database);
try (PreparedStatement preparedStatement = connection.prepareStatement(stringBuilder.toString())) {
preparedStatement.executeUpdate();
}
Is there any other way to create a query in java?