我读过关于在数据库中存储对象的H2文档。有特殊的SQL类型OTHER和方法setObject
和getObject
。我试过这段代码:如何将对象插入到h2
PreparedStatement statement = null;
try {
statement = connection.prepareStatement("CREATE TABLE PUBLIC.foo (name VARCHAR(64) NOT NULL, data OTHER NULL);");
statement.execute();
} finally {
statement.close();
}
statement = null;
try {
statement = connection.prepareStatement("INSERT INTO PUBLIC.foo (name, data) VALUES(?,?);");
statement.setString(1, "lololo");
statement.setObject(2, new String[]{"foo", "bar"});
statement.execute();
}finally {
statement.close();
}
但我有例外:
org.h2.jdbc.JdbcSQLException:ШÐμÑÑ,наÐ'цd° ÑõиричннÑÑÑÑÑÑÑÑÑÑмÐвоÐвÐнÐнÐнÐнÐннÐнÐнÐÐнÐÐÐнÐÑ “(foo,bar)” 十六进制字符串包含非十六进制字符:“(foo,bar)”; SQL语句:(?) INSERT INTO PUBLIC.foo(名称,数据)VALUES - (?1,2)[90004-191]
有什么不对?
看看这里:http://stackoverflow.com/questions/31964209/h2-other-data-type-throws-exception-when-storing -string-or-boolean – Seelenvirtuose
非常感谢。 –