user1993412
user1993412

Reputation: 862

Java : Passing null as Long in PreparedStatement (JDBC)

I have a variable which is

Long val = null;

And I am looking for a way to pass this to PreparedStatement statement object in order to set the field value in the database as null.

statement.setLong(1,val);

It does not work for Long, but only long.

Any inputs ideas, or suggestions.

Thanks !!!

Upvotes: 5

Views: 9236

Answers (1)

rgettman
rgettman

Reputation: 178263

You should use the setNull method.

statement.setNull(1, Types.BIGINT);  // Types.BIGINT maps to 64-bit long

When mapping Java types to JDBC types, this article states:

The recommended Java mapping for the BIGINT type is as a Java long.

Upvotes: 19

Related Questions