Java – how to set a formula in JDBC Preparedstatement

I have a Preparedstatement that I use to insert a pile of rows containing a large amount of column data However, for one of the columns (timestamp), if the value is null, I want it to execute getdate () on the server

I know I can do the new date (), but it won't really work because, due to the legacy report, it needs to match another column with automatic insertion value

We also said that I could not change the schema and add default values for columns

Is there any way to set a formula as a parameter in Preparedstatement?

Edit and add: This is basically its current situation:

PreparedStatement statement = connection.prepareStatement("insert into row (name,starttime) values (?,?)")
statement.setString(1,name);
statement.setDate(2,date);

What I want is, like:

PreparedStatement statement = connection.prepareStatement("insert into row (name,name);
if(date != null)
  statement.setDate(2,date);
else
  statement.setFormula(2,"getdate()");

I understand that:

if(date == null)
  date = new Date();

It's basically the same, but in our case, it's really not

resolvent

Solution

Simply prepare the form to see if the declaration is valid

insert into row(name,starttime)
   values(?,coalesce(?,getdate())

? Then set null. Should the second parameter result in a getdate () call?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>