You cannot create it without connecting to the database. A PreparedStatementwill be precompiled in the database and therefore really needs an open connection.
Instead, you can simply consider dynamically assembling an SQL string. Generating placeholders PreparedStatement(objects ?) in a loop and using String#format()put them in an SQL string. u may also consider simply passing the variables to your method runSQLResultSetand building there instead.
According to the comments, here is an example:
try {
connection = database.getConnection();
statement = connection.prepareStatement(SQL);
setValues(statement, values);
// ...
.
public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
for (int i = 0; i < values.length; i++) {
preparedStatement.setObject(i + 1, values[i]);
}
}
source
share