MySQL uses “IS NULL” instead of “NULL” so how do i bind variable to achieve the same ?
Right now i tried with this
selectStmt.setNull( 2, java.sql.Types.VARCHAR );
when i printed “java.sql.Types.VARCHAR” shows value = 12 ? If so how does this set ” IS NULL” in preparedstatment ?
MORE DETAILS:
i.e I am interested in querying (MySQL) for null if i have string = ” ” in input. So logic goes like this,
Connection c = SQLCon.connect();
PreparedStatement selectStmt = c.prepareStatement( "select * from "
+ TableName
+ " where P = ? AND M = ? AND PR = ? AND DR = ? " );
if ( P.trim().equals( "" ) )
{
selectStmt.setNull( 1, java.sql.Types.VARCHAR );
} else
{
selectStmt.setString( 1, P );
}
if ( M.trim().equals( "" ) )
{
selectStmt.setNull( 2, java.sql.Types.VARCHAR );
} else
{
selectStmt.setString( 2, M );
}
Please advice
If you want to match a column in a where clause that might be null with a parameter that might be null, you actually need to bind it twice, because in SQL a null doesn’t equal anything else, even another null.