Problem filling a java.util.Date object into column with type "OTHER"

315 views
Skip to first unread message

A. Christian

unread,
Jun 22, 2012, 9:04:42 AM6/22/12
to H2 Database
Hi there,

I'm creating a kind of POJO persistence API with H2 as the backend.
But I'm facing the following error when I try to set a java.util.Date
object into a "other" column:

----
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Hexadezimal
Zahl mit einer ungeraden Anzahl Zeichen: "2012-06-22 14:57:36.885"
Hexadecimal string with odd number of characters: "2012-06-22
14:57:36.885"; SQL statement:
INSERT INTO test VALUES (?,?) -- (?1, ?2) [90003-166]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:
329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.util.StringUtils.convertHexToBytes(StringUtils.java:990)
at org.h2.value.Value.convertTo(Value.java:823)
at org.h2.table.Column.convert(Column.java:143)
at org.h2.command.dml.Insert.insertRows(Insert.java:112)
at org.h2.command.dml.Insert.update(Insert.java:84)
at org.h2.command.CommandContainer.update(CommandContainer.java:73)
at org.h2.command.Command.executeUpdate(Command.java:226)
at
org.h2.jdbc.JdbcPreparedStatement.execute(JdbcPreparedStatement.java:
181)
at de.root1.sos.SqlTest.main(SqlTest.java:30)
----

Sample to reproduce this issue:

----
package de.root1.sos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.UUID;

public class SqlTest {
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:h2:/tmp/test;FILE_LOCK=SERIALIZED";
Connection connection = DriverManager.getConnection(jdbcUrl,
"sa", "");
Statement createTableStmnt = connection.createStatement();
createTableStmnt.executeUpdate("CREATE TABLE test (id UUID
PRIMARY KEY, dateObject OTHER);");
PreparedStatement prepareStatement =
connection.prepareStatement("INSERT INTO test VALUES (?,?)");
prepareStatement.setObject(1, UUID.randomUUID());
prepareStatement.setObject(2, new Date());
prepareStatement.execute();
}
}
----

What's wrong with the code?

I know, it would be somehow better to use the "DATE" type for storing
dates, but for this scenario I need "OTHER" type (Object) ...


Any ideas?

br,
Alex

A. Christian

unread,
Jun 22, 2012, 12:29:49 PM6/22/12
to H2 Database
Found the solution myself:

Instead of

prepareStatement.setObject(2, new Date());

one can explicitly tell JDBC that this object has to be handled as
type Java Object:

prepareStatement.setObject(2, new Date(), Types.JAVA_OBJECT);

Works now as expected...
Reply all
Reply to author
Forward
0 new messages