H2Database - using mssql DATETIMEOFFSET data type - java

I totally fail when i want use this data type. I get exception:
Unknown data type: "DATETIMEOFFSET"; SQL statement:
ALTER TABLE PUBLIC.usr_user ALTER COLUMN next_password_change_date DATETIMEOFFSET [50004-196]
Is there any way around that?

Unfortunately, there is no DATETIMEOFFSET data type in h2 like in SQL server.
Reference: http://www.h2database.com/html/datatypes.html
Way around that will be to choose a different date type e.g. DATE, TIMESTAMP or TIMESTAMP WITH TIME ZONE.

Related

MySQL/Hibernate retrieving wrong data with column mapped as LocalTime

I'm mapping a TIME column of a MySQL database in Hibernate (5.4.6.Final) as a java.time.LocalTime class.
This is the declaration of the mapping into the entity:
#Column(name = "TIMEVAL")
public LocalTime TIMEVAL;
The column in the database has value 00:30:00, however Hibernate it's building the instance of TIMEVAL with a value of 01:30:00.
I thought about a difference of time-zones (Ignoring the fact that the TIME data-type doesn't have a time-zone) between the server and the hibernate connection, but I setup each connection and the server as UTC.
And by executing the queries:
SELECT ##global.time_zone;
SELECT ##session.time_zone;
The result of the Hibernate session and the server were all +00:00.
Following the Hibernate configuration:
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.datasource.url=jdbc:mysql://localhost:3306/..?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&allowMultiQueries=true
And here's the MySQL configuration:
default-time-zone=+00:00
I tried also:
Specifying the MySQL column as VARCHAR(8), but Hibernate always retrieves 01:30:00;
Changing the Entity data-type to Duration, but Hibernate throws an error saying that it can't map TIME to java.lang.Long.
What do I need to modify in order to have Hibernate build the LocalTime instance with the value of the database column?
As #Andreas pointed out in the comments. The JVM time zone wasn't in UTC.
I needed to change the JVM time zone to get the correct value out of the database.
So, by adding the following param to the JVM options:
-Duser.timezone=UTC
Finally the LocalTime instance was built with the value of 00:30:00 as the value of the database column.

Timestamp insert issue in Mysql from Oracle

I am trying to insert the timestamp value from oracle to mysql timestamp column.
oracle value is 2017-09-01 11:35:22.495000000 but while getting value from result set its giving 2017-09-01 11:35:22.495.
its stored in oracle using oracle.sql.timestamp and i cannot insert the value in mysql.so getting stringvalue or timestamp value from oracle.sql.timestamp API.
But mysql storing the value is 2017-09-01 11:35:22.000495 and datatype defined as timestamp (6) and am not sure why its inserting the value like this?
How i can store the value in mysql similar to oracle ?
Using JDBC you should be able to directly copy a timestamp from one database to another doing something like this:
try(Connection oracleConnection = getOracleConnection();
Connection mysqlConnection = getMySQLConnection();
PreparedStatement oracleStmt = oracleConnection.prepareStatement("SELECT my_time FROM oracle_table");
PreparedStatement mysqlStmt = mysqlConnection.prepareStatement("INSERT INTO mysql_table VALUES (?)");
ResultSet rs = oracleStmt.executeQuery()) {
while(rs.next) {
mysqlStmt.setTimestamp(1, rs.getTimestamp("my_time"));
mysqlStmt.execute();
}
}
Timestamps are essentially numeric datatypes. Different DBMS's can have different precisions and different ways of handling timezones, but you shouldn't need any database specific API's to interact with them in most cases.
If you need to format a Timestamp you can use SimpleDateFormat on what you get back from getTimestamp() to format the string any way you need to.

You have an error in your SQL syntax;..(in the alter command)

Please tell me what causes error in this sql query given below:-
ALTER TABLE message ADD COLUMN sent_at timestamptz DEFAULT NOW();
timestamptz is not a MySQL data type. timestamp is a MySQL data type.
You have an error don't need column
ALTER TABLE message ADD sent_at timestamptz DEFAULT NOW();

Hibernate SQL - Can't get an interval from table

I'm trying to get an interval through an operation with Timestamps from a table but I'm getting a "No Dialect mapping for JDBC type: 1111"
The code is like this:
SELECT TS_FINISH - TS_START FROM TABLE
Where TS_FINISH and TS_START are Timestamps
I used a function from my DBMS to cast the result to a text.
Unfortunately there is no hql function that does it.

Resultset.getDate() throwing exception java.lang.NumberFormatException: For input string: "Name of Month"

When extracting date from database using Resultset.getDate(), I am getting the following exception:
java.lang.NumberFormatException: For input string: "Name of Month".
I can confirm that the exception is thrown when trying to get data from a column of Oracle date datatype.
I can't paste the actual code here as it is very long. But the sample code is as given below
pstmnt = connection.prepareStatement(selectQuery);
rs = pstmnt.executeQuery();
while (rs.next()) {
rs.getDate(column.getColName());
}
Please help?
As requested by Sanjay, please find the stack trace below
java.lang.NumberFormatException: For input string: "MAY"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:531)
at java.sql.Date.valueOf(Date.java:200)
at oracle.jdbc.driver.OracleStatement.getDateValue(OracleStatement.java:4610)
at oracle.jdbc.driver.OracleResultSetImpl.getDate(OracleResultSetImpl.java:625)
at oracle.jdbc.driver.OracleResultSet.getDate(OracleResultSet.java:1601)
at quotecopy.DbConnection.getTableRows(DbConnection.java:126)
at quotecopy.QuoteCopier.main(QuoteCopier.java:66)
java.lang.NumberFormatException: For input string: "MAY"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:63)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:531)
at java.sql.Date.valueOf(Date.java:200)
at oracle.jdbc.driver.OracleStatement.getDateValue(OracleStatement.java:4610)
at oracle.jdbc.driver.OracleResultSetImpl.getDate(OracleResultSetImpl.java:625)
at oracle.jdbc.driver.OracleResultSet.getDate(OracleResultSet.java:1601)
at quotecopy.DbConnection.getTableRows(DbConnection.java:127)
at quotecopy.QuoteCopier.main(QuoteCopier.java:66)
Can you check your query, i believe you are using to_char on the Date column which is causing the getDate unable to recognize it as a valid Date
please refer http://forum.springsource.org/showthread.php?19783-Oracle-JDBC-Date-Problem.
May be it would help you
rs.getString() or rs.getDate() retrieves based on your database table column name. Can you verify that the column containing the date is named "Name of Month" ?
A few things you might want to check out:
Is the column type of this column in database really date/datetime?
What happens when you print out getObject() instead of getDate()? Can you show us the output?
What happens when you use column indices to retrieve the date instead of using column name?
Based on the information provided, it would be worth checking for the following scenarios in the codebase:
Reading a column that is not of type DATE in the Oracle database. The Oracle JDBC driver maps the Oracle database type DATE to the java.sql.Date type; actually it is the oracle.sql.Date type, which is a subtype of the java.sql.Date type. If you are not reading a column of type Oracle DATE, but instead a TIMESTAMP field, then you might encounter the exception. You would therefore have to use a suitable method - likely to be getTimeStamp, to read the data in the column.
The NLS_DATE_FORMAT used in the database is different from the date format expected by the JDBC driver. This would either require you to change the NLS_DATE_FORMAT on creating a session, using a logon trigger, or it would require you to use TO_CHAR function calls with appropriate date formats in your SQL queries.
Accessing the columns in the ResultSet in a random manner. Most JDBC drivers will expect you to read the contents of the result set, column by column starting at the first column, without allowing you to ignore any columns. If you attempted reading a column while ignoring the previous ones, then you might encounter this exception.

Categories

Resources