SQLite JDBC 3.30.1(最新)不支持java.time。例如,
创建表Foo(id TEXT,time1 NUMERIC,time2 NUMERIC);
PreparedStatement stmt = connection.prepareStatement("insert into Foo(id, time1, time2) values (1, ?, ?)");
stmt.setParameter(1, LocalDate.now());
stmt.setParameter(2, LocalTime.now());
stmt.executeUpdate();
成功。现在尝试检索该行:
PreparedStatement stmt = connection.prepareStatement("select id, time1, time2 from Foo");
ResultSet resultSet = stmt.executeQuery();
resultSet.getObject(2, java.time.LocalDate.class)
resultSet.getObject(2, java.time.LocalTime.class)
投掷
java.sql.SQLFeatureNotSupportedException
有人面临同样的问题并有解决方案吗?
令人惊讶的是,resultSet.getObject(2, java.time.LocalDate.class)
确实确实抛出了SQLFeatureNotSupportedException
。但是,您可以使用.getString
检索String表示形式,然后使用.parse
进行后续转换:
stmt = conn.prepareStatement("select id, time1, time2 from Foo");
ResultSet resultSet = stmt.executeQuery();
String ldString = resultSet.getString(2);
LocalDate ld = LocalDate.parse(ldString);
String ltString = resultSet.getString(3);
LocalTime lt = LocalTime.parse(ltString);
System.out.println(ld); // 2020-03-22
System.out.println(lt); // 15:53:49.284