PreparedStatement and 'null' value in WHERE clause

Posted by bond on Stack Overflow See other posts from Stack Overflow or by bond
Published on 2012-04-03T15:34:38Z Indexed on 2012/04/03 17:30 UTC
Read the original article Hit count: 147

Filed under:

I am using PrepareStatement and BatchUpdate for executimg a UPDATE query. In for loop I create a Batch. At end of loop I execute batch.

Above logic works fine if SQL query used in PrepareStatement does not have null values in WHERE claues.

Update Statement fails if there is null value in WHERE clasue.

My code looks something like this,

connection = getConnection();

PreparedStatement ps = connection.prepareStatement("UPDATE TEST_TABLE
SET Col1 = true
WHERE
Col2 = ? AND Col3 = ?")

for (Data aa : InComingData)
{
if(null == aa.getCol2())
{
ps.setNull(1, java.sql.Types.INTEGER);
}
else
{
ps.setInteger(1,aa.getCol2())
}

if(null == aa.getCol3())
{
ps.setNull(2, java.sql.Types.INTEGER);
}
else
{
ps.setInteger(2,aa.getCol3())

}

ps.addBatch();

}

ps.executeBatch();
connection.commit();

Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about jdbc