LocalDateTime and SQL Server JDBC 4.2 driver - java

I'm trying to use new java.time classes with most recent version of Sql Server JDBC driver. As I read it should just work with methods: PreparedStatement.setObject() and ResultSet.getObject().
So I created sample code, and can't get it work with ResultSets. I don't know what I'm doing wrong here.
Connection connection = DriverManager.getConnection(connectionString);
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM myTable WHERE ? BETWEEN date_from AND date_to");
preparedStatement.setObject(1, LocalDateTime.now()); // That works
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Object o = resultSet.getObject("date_from");
o.getClass() returns java.sql.Timestamp
LocalDateTime dateTime = resultSet.getObject("date_from", LocalDateTime.class);
}
This throws an exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The conversion to class java.time.LocalDateTime is unsupported.
Driver version: mssql-jdbc-6.5.4.jre8-preview.jar
SQL Server version: 2016
https://learn.microsoft.com/en-us/sql/connect/jdbc/jdbc-4-2-compliance-for-the-jdbc-driver?view=sql-server-2017
How to interpret this sentence in table at bottom:
New Java classes in Java 8:
LocalDate/LocalTime/LocalDateTime,
OffsetTime/OffsetDateTime
New JDBC types:
TIME_WITH_TIMEZONE,
TIMESTAMP_WITH_TIMEZONE,
REF_CURSOR
REF_CURSOR is not supported in SQL Server. Driver throws a SQLFeatureNotSupportedException exception if this type is used. The driver supports all other new Java and JDBC type mappings as specified in the JDBC 4.2 specification.

I don't know what I'm doing wrong here.
You're not doing anything wrong. You have encountered a deficiency in Microsoft's JDBC driver for SQL Server prior to version 7.1.0, discussed here.
If you are using mssql-jdbc version 7.1.0 or later then you can use getObject(x, LocalDateTime.class) as expected.
For mssql-jdbc versions prior to 7.1.0, as others have suggested, you'll need to retrieve a Timestamp and convert it to a LocalDateTime. However, be aware that the simplistic solution ...
LocalDateTime dateTime = resultSet.getTimestamp("date_from").toLocalDateTime()
... will corrupt certain date/time values if the default time zone for the JVM observes Daylight Saving Time, a.k.a. "Summer Time". For example,
// time zone with Daylight Time
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));
// test environment
Statement st = conn.createStatement();
st.execute("CREATE TABLE #tmp (id INT PRIMARY KEY, dt2 DATETIME2)");
st.execute("INSERT INTO #tmp (id, dt2) VALUES (1, '2018-03-11 02:00:00')");
ResultSet rs = st.executeQuery("SELECT dt2 FROM #tmp WHERE id=1");
rs.next();
// test code
LocalDateTime x = rs.getTimestamp("dt2").toLocalDateTime(); // bad
System.out.println(x.toString());
will print "2018-03-11T03:00". Note that the time is "03:00", not "02:00".
Instead, you'll need to retrieve the Timestamp as UTC and then convert it into a LocalDateTime for UTC, thus removing the time zone component
// time zone with Daylight Time
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));
// test environment
Statement st = conn.createStatement();
st.execute("CREATE TABLE #tmp (id INT PRIMARY KEY, dt2 DATETIME2)");
st.execute("INSERT INTO #tmp (id, dt2) VALUES (1, '2018-03-11 02:00:00')");
ResultSet rs = st.executeQuery("SELECT dt2 FROM #tmp WHERE id=1");
rs.next();
// test code
Timestamp ts = getTimestamp("dt2", Calendar.getInstance(TimeZone.getTimeZone("UTC")));
LocalDateTime x = LocalDateTime.ofInstant(ts.toInstant(), ZoneId.of("UTC")); // good
System.out.println(x.toString());
which prints "2018-03-11T02:00".

This is because the Microsoft SQL Server JDBC driver implementation of resultSet.getObject(...) cannot auto convert from java.sql.Timestamp to LocalDateTime.
As a workaround you can get the value as java.sql.Timestamp and then convert java.sql.Timestamp to LocalDateTime by using: java.sql.Timestamp.toLocalDateTime()
LocalDateTime dateTime = resultSet.getTimestamp("date_from").toLocalDateTime()

It looks like the mssql-jdbc driver doesn't fully implement the java.time support specified in JDBC 4.2. It doesn't support it for ResultSet.getObject, but it does support it for PreparedStatement.setObject.
The workaround, as suggested by the answer of Loc Le, is to retrieve as Timestamp and convert that to a LocalDateTime.

If you are stuck and want to try the "old way" of doing it:
Here is a simples example of equivalent conversions. Test yourself.
#Test
public void testingConversionTimestampAndLocalDateTime(){
Timestamp initialTimestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = LocalDateTime.ofInstant(initialTimestamp.toInstant(), ZoneId.of("America/Sao_Paulo"));
ZoneOffset zoneOffset = ZoneId.of("America/Sao_Paulo").getRules().getOffset(localDateTime);
Timestamp timestamp = Timestamp.from(localDateTime.toInstant(zoneOffset));
Timestamp timestamp1 = Timestamp.valueOf(localDateTime);
assertEquals(initialTimestamp, timestamp);
assertEquals(initialTimestamp, timestamp1);
}

Related

How to create SQL date object in a desired format ( MM/DD/YYYY HH:MM:SS AM ) in Java?

In the database server, the format of the date column is MM/DD/YYYY HH:MM:SS AM
How can I create the SQL date object to send to database in the same format mentioned above?
Type of the database column is DATE. I am using Oracle Database 12C : SQL.
DATE or TIMESTAMP columns do not have "a format".
As your value apparently contains a time, you should use LocalDateTime or java.sql.Timestamp to retrieve that value.
Something like:
String sql = "select the_timstamp_column from the_table";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
LocalDateTime tsValue = rs.getObject(1, LocalDateTime.class);
}
But still not all JDBC drivers support that, so the second best solution is to use java.sql.Timestamp:
String sql = "select the_timstamp_column from the_table";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
java.sql.Timestamp tsValue = rs.getTimestamp(1);
}
In both cases you can format the value of tsValue anyway you like in your Java code.

java ucanaccess data storing

i tried to store in two column current data and time on my microsft access database from java, but when i open my file no data has been stored. I tried to print the columns but it print "null".
How can i do?
Date date = new Date();
final String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
final String formattedTime = new SimpleDateFormat("HH:mm:ss").format(date.getTime());
final java.sql.Date sqlDate = java.sql.Date.valueOf(formattedDate);
final java.sql.Time sqlTime = java.sql.Time.valueOf(formattedTime);
...
PreparedStatement ps=con.prepareStatement("insert into Table1(Data) values(?)");
ps.setDate(1,sqlDate);
ps=con.prepareStatement("insert into Table1(Hour) values(?)");
ps.setTime(1,sqlTime);
ps.executeUpdate();
This is the printed result :
ID Name Date Hour
0001 Mary null null
A couple of points:
As far as I can tell, there is no point in keeping your date and your time in two separate columns in your database table. I would use one column of datatype datetime.
The classes java.util.Date, SimpleDateFormat, java.sql.Date and Time are long outdated and poorly designed. Better to use java.time, the modern Java date and time API. It is so much nicer to work with.
Taking for granted that date and time are in separate columns your code may look like this:
LocalDateTime dateTimeNow = LocalDateTime.now(ZoneId.of("Europe/Rome"));
LocalDate dateToday = dateTimeNow.toLocalDate();
LocalTime timeNow = dateTimeNow.toLocalTime();
PreparedStatement ps
= con.prepareStatement("insert into Table1(Data, Hour) values(?, ?)");
ps.setObject(1, dateToday);
ps.setObject(2, timeNow);
ps.executeUpdate();
This will insert one row containing both the current day and the current time. Please use your desired time zone where I put Europe/Rome since both date and time depend on time zone.
Link: Oracle tutorial: Date Time explaining how to use java.time.

preparedStatement with TimeStamp compare two date

i have this code
preparedStatement = jdbcManager.getConnection().prepareStatement(query);
Date start; /*get from postgres column type-> Timestamp without time zone*/
java.sql.Timestamp timestamp = new java.sql.Timestamp(start.getTime());
log.debug("Parametro d'ingresso query: "+timestamp);
preparedStatement.setTimestamp(1, timestamp);
and i have this query
SELECT
DATA
FROM MY_TABLE
WHERE DATA > ?
the column DATA is DATA_TYPE = DATE in a Oracle db
the compare in the query not working,
what am I doing wrong?
You're comparing date with timestamp. Instead of fetching time stamp, get java.sql.Date. And also in prepared statement, use setDate instead of setTimeStamp.

datetime in sql changes to just date in my application

I have a column in my table from type datetime, I am retrieve that column to my java application using jdbc.
my problem is that when I stored 2014-05-13 23:49:18.150 in my database, it becomes 2014-04-13 in my java application.
how to get all the date include time (house, seconds , minutes ).
java code
callableStatement = con
.prepareCall("{call getStatusForOrder(?,?,?,?,?)}");
callableStatement.setInt(1, order.getID());
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(3, java.sql.Types.DATE);
callableStatement.registerOutParameter(4, java.sql.Types.DATE);
callableStatement.registerOutParameter(5, java.sql.Types.BIT);
callableStatement.execute();
int statusID = callableStatement.getInt(2);
Date startTime = callableStatement.getDate(3);
Date endTime = callableStatement.getDate(4);
boolean isActive = callableStatement.getBoolean(5);
System.out.println("IsActive = " + isActive);
System.out.println("Start Date = " + startTime.toString());
Use the Timestamp instead of Date, both datetime and datetime2 map to java.sql.Timestamp.
See the documentation.
PS. Do not forget to use callableStatement.getTimestamp instead of callableStatement.getDate.
http://docs.oracle.com/javase/6/docs/api/java/sql/CallableStatement.html#getTimestamp(int)
Use a DateFormat to display it.
SHORT is completely numeric, such as 12.13.52 or 3:30pm
MEDIUM is longer, such as Jan 12, 1952
LONG is longer, such as January 12, 1952 or 3:30:32pm
FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
I think you must use Timestamp (if I recall it right, haven't programmed Java for a while) instead of Date
Types.DATE signifies a JDBC (and SQL) datatype that only has a date component, and no time. You need to use Types.TIMESTAMP and the associated get/setTimestamp(..) methods and class java.sql.Timestamp if you want to have the time portion included.

inserting datetimes into database using java

I am wanting to insert a datetime into a MySql data base using Java and a prepared statement:
Calendar cal = Calendar.getInstance();
PreparedStatement stmnt = db.PreparedStatement("INSERT INTO Run " +
"(Time) VALUE (?) ");
stmnt.setDate(1, new java.sql.Date(cal.getTime()));
stmnt.executeQuery();
NOTE: there is currently an error - cannot find symbol (java.sql.Date) line 4 here
db is an instance of a sort of wrapper class that exposes what I need from java.sql - its just getting a prepared statement from my connection object here.
Time (the column) is a date time in my database, and I can only see setDate and setTime method but I want to store both - also my code does not work anyway ;-)
If anyone could give me some pointers on inserting a combined date time (current time would be a great help as that's my first goal) into a MySql DB using a prepared statement I would be very grateful.
Thanks
The constructor for java.sql.Date takes a long (milliseconds since 1970) java.sql.Date
To get milliseconds from a java.uitl.Calendar, you use cal.getTimeInMillis()
Your code would be:
Calendar cal = Calendar.getInstance();
PreparedStatement stmnt = db.PreparedStatement("INSERT INTO Run " + "(Time) VALUE (?) ");
stmnt.setDate(1, new java.sql.Date(cal.getTimeInMillis()));
stmnt.executeQuery();
the following code should allow you to insert a date with millisecond accuracy. I have used it with HSQLDB, Sybase, SQL-Server and MySql without any problems.
java.util.Date date = getMyDate();
if (date == null) {
statement.setNull(insertionIndex, Types.TIMESTAMP);
} else {
statement.setTimestamp(insertionIndex, new Timestamp (date.getTime()));
}

Categories