Juro que esto solía funcionar, pero no es en este caso. Estoy intentando hacer coincidir col1, col2 y col3, incluso si uno o más de ellos es nulo. Sé que en algunos idiomas he tenido que recurrir a circumlocutions como ((? is null AND col1 is null) OR col1 = ?)
. ¿Es eso requerido aquí?Enlazando una variable nula en un Estado Preparado
PreparedStatement selStmt = getConn().prepareStatement(
"SELECT * " +
"FROM tbl1 " +
"WHERE col1 = ? AND col2 = ? and col3 = ?");
try
{
int col = 1;
setInt(selStmt, col++, col1);
setInt(selStmt, col++, col2);
setInt(selStmt, col++, col3);
ResultSet rs = selStmt.executeQuery();
try
{
while (rs.next())
{
// process row
}
}
finally
{
rs.close();
}
}
finally
{
selStmt.close();
}
// Does the equivalient of stmt.setInt(col, i) but preserves nullness.
protected static void setInt(PreparedStatement stmt, int col, Integer i)
throws SQLException
{
if (i == null)
stmt.setNull(col, java.sql.Types.INTEGER);
else
stmt.setInt(col, i);
}
Véase también http://en.wikipedia.org/wiki/Null_%28SQL%29#Three-valued_logic_.283VL.29 – trashgod