public static void setValues(PreparedStatement statement, Object... values) throws SQLException
{
for(int i = 0; i < values.length; i++)
{
if(values[i] instanceof Integer)
{
int value = (Integer) values[i];
statement.setInt(i, value);
}
else if(values[i] instanceof String)
{
String value = (String) values[i];
statement.setString(i, value);
}
}
}
public static ResultSet query(String query, Object... values) throws Exception
{
statement = connect.prepareStatement(query);
setValues(statement, values);
return statement.executeQuery();
}
...
String query = "SELECT * FROM words WHERE word LIKE ?";
resultSet = query(query, name); // <-- this line gives following error and name is String with content I have checked that
...
java.sql.SQLException: Parameter index out of range (0 < 1 ).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3321)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3310)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4146)
at org.Program.main.Program.setValues(Program.java:43)
at org.Program.main.Program.query(Program.java:51)
at org.Program.main.Program.updateCounter(Program.java:66)
at org.Program.main.Program.getAll(Program.java:156)
at org.Program.main.Program.main(Program.java:234)