Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert NULL into setDouble method Java

My code attempting to insert NULL into setDouble method in PreparedStatement

ps.setDouble(++i, balance.getAmount());

getAmount() is Double field.

i am able to handle like

if (balance.getAmount() ==  null ) {
    ps.setNull(++i, java.sql.Types.NULL);
}else{
    ps.setDouble(++i, balance.getAmount());
}

But my code looks ugly, because my code have many number of setInt setLong setDouble methods.

Is there any way to hanlde smartly or is there any other approach. Please advise me.

like image 839
deadend Avatar asked Oct 30 '25 06:10

deadend


1 Answers

You could try

preparedStatement.setObject(i, balance.getAmount(), java.sql.Types.DOUBLE);

setObject(int parameterIndex, Object x, int targetSqlType)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!