Manisha GPT
Manisha GPT

Reputation: 21

How to replace Null with empty string in Java insert prepared statement

I have API response which I am using Java insert prepared statement to insert values in DB table. Few column values coming in from API response is- null. I want to replace this null value with empty string in my table. Can someone help with the piece of code.

Code snippet:

PreparedStatement stmt = conn.prepareStatement(query);
stmt.setDate(1, formatDate(obj.getString("term_dt")));

Upvotes: 2

Views: 1804

Answers (2)

magicmn
magicmn

Reputation: 1914

You can use Objects.toString(obj.getString("term_dt"), ""). It's a bit shorter than using a ternary operator.

Upvotes: 1

Ankit Beniwal
Ankit Beniwal

Reputation: 529

You can use the ternary operator to keep your code concise.

PreparedStatement stmt = conn.prepareStatement(query);
stmt.setDate(1, term_dt!=null ? formatDate(obj.getString("term_dt")) : "");

Upvotes: 1

Related Questions