JDBC4 insert the date "0001-01-01"

I want to insert "0001-01-01" as the value in the date field using Java PreparedStatement .

But this throws an exception when I tried this:

 String sql = "insert into mytable values(?)" ps = conn.prepareStatement(sql); ps.setDate(1, java.sql.Date.valueOf("0001-01-01")); ps.executeUpdate(); // throws exceptions here. 

Error:

The value shown is not a valid instance of the datetime data type. Check the source data for invalid values. An example of an invalid value is numeric data with a scale that exceeds accuracy.

  • If I do not use PreparedStatement, I can insert "0001-01-01". However, make does not seem to allow me to insert this value.

  • It will work if I inserted "1969-01-01" instead of "0001-01-01."

Any ideas?

Updates: Here is more information you may need.

  • we are using SQL Server 2012.
  • we must use "0001-01-01" because these values ​​already existed. I use very old codes to use the preparation instructions. Therefore, I need to insert the same values ​​into the same functionality.

Updates 2:

In addition, I can insert β€œ0001-01-01” into the date field without using the preparation instructions. i.e.

  String sql = "insert into mytable values('0001-01-01')" java.sql.Statement statement = conn.createStatement(); statement.executeUpdate(sql); 

So this is not sql server problem or db field problem.

+5
source share
2 answers

Have you tried with the new java.sql.Date (new date (1,1,1))?

0
source

Try using another suitable JDBC driver.

0
source

Source: https://habr.com/ru/post/1232830/


All Articles