A JDBC parameter or column is addressed with index 0 or less, but they are numbered from 1
What it finds
JDBC numbers the parameters of a PreparedStatement and the columns of a ResultSet from 1. statement.setString(0, name) or rows.getInt(0) throws SQLException at runtime, in code that the compiler accepts and that often only runs in production.
Reported for the setters, getters and updaters that take an index, when the index is a literal of 0 or less and the receiver is known to be a statement or a result set: by its declared type, by the call that made it (prepareStatement, executeQuery and the like), or as the parameter of use on such a call. Files that import nothing from java.sql are left alone.
From the rule's source, jdbc_index_zero.rs.
import java.sql.Connectionimport java.sql.ResultSet fun load(connection: Connection, rows: ResultSet, form: Form) { val statement = connection.prepareStatement("select name from users where id = ?") statement.setInt(0, 7)tx0 reports: setInt is given index 0, but JDBC counts from 1 statement.setInt(1, 7) val name = rows.getString(0)tx0 reports: getString is given index 0 connection.prepareStatement("select 1").use { it.setString(0, "x") }tx0 reports: index 0 statement.executeQuery().use { result -> result.getLong(0) }tx0 reports: index 0 form.setField(0, "x") rows.getString(1)} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:jdbc-index-zero" = "error"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:jdbc-index-zero] reason="..."tx0 explain tx0/kotlin:jdbc-index-zero shows the same in the terminal. Found a false positive? Report it.