【发布时间】:2016-08-10 13:14:39
【问题描述】:
MySQL 版本:5.5.41-0ubuntu0.14.04.1
根据docs,Time 的最大值为“838:59:59”
我将此值插入到名为 table01 的表中,该表有 1 个 Time 列 col_time。
我尝试使用 Java JDBC 代码获取此内容:
public class JDBCUtil {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://xx.xx.xx.xx:3306/testdb";
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args) {
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
}
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();) {
String sql = "select * from table01;";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Time time = rs.getTime("col_time");
System.out.print("col_time : " + time);
}
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
我遇到了异常:
java.sql.SQLException:第 1 列中时间“838:59:59”的格式错误 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 在 com.mysql.jdbc.ResultSetRow.getTimeFast(ResultSetRow.java:949) 在 com.mysql.jdbc.ByteArrayRow.getTimeFast(ByteArrayRow.java:226) 在 com.mysql.jdbc.ResultSetImpl.getTimeInternal(ResultSetImpl.java:6050) 在 com.mysql.jdbc.ResultSetImpl.getTime(ResultSetImpl.java:5784) 在 com.mysql.jdbc.ResultSetImpl.getTime(ResultSetImpl.java:5819) 在 com.impetus.idw.blend.utils.JDBCUtil.main(JDBCUtil.java:34)
这超出了java.sql.Time的限制
然后我使用
将其作为字符串提取while (rs.next()) {
String time = rs.getString("col_time");
System.out.print("col_time : " + time);
}
我遇到了异常:
java.sql.SQLException:第 1 列中时间“838:59:59”的格式错误 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927) 在 com.mysql.jdbc.ResultSetImpl.getTimeFromString(ResultSetImpl.java:5975) 在 com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5723) 在 com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570) 在 com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5610) 在 com.impetus.idw.blend.utils.JDBCUtil.main(JDBCUtil.java:37)
如何获取 时间 字段?我在这里错过了什么?
【问题讨论】:
-
时间的格式是什么?您可以尝试导入不同的时间数据类型吗?查看此链接:db.apache.org/derby/docs/10.4/ref/rrefsqlj21908.html
-
这是另一个链接,格式为
Time变量:forcedotcom.github.io/phoenix/datatypes.html#time_type -
@LakshyaGoyal 我没明白你的意思。我有客户提供的数据。我无法更改数据类型。我想使用 JDBC 读取这些数据。
-
代码中的错误到底在哪里?虽然行?