java - 我如何从Java中的MySql获取时间?

标签 java mysql time

我的表中有一列包含时间数据类型。我需要打印它的值(value)。这是我正在使用的代码:

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM student");
System.out.print("\t\t"+rs.getTime("Total Time"));

输出是 00:00:00,但我数据库中的实际值是 24:00:00。我怎样才能得到正确的值(value)?

最佳答案

调用rs.next()在使用 getTime() 将光标向前移动到 first 行之前:

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM student");
if (rs.next()) {
  System.out.print("\t\t"+rs.getTime("Total Time"));
}

来自文档:

A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

关于java - 我如何从Java中的MySql获取时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12656486/

相关文章:

java - 设置堆栈 Pane 中 Pane 的对齐方式

java - 发布 key 无法与已部署的应用程序一起使用

python - while 循环中的 time.sleep 函数不断导致程序崩溃,解决方案还是替代方法?

macos - 时间命令的非法选项

java - Logback-Slf4j 从 IDe 运行时将日志消息写入文件,但不是作为 jar

java - 无法编译 Java + Play! 'play eclipse' 上的框架项目

php - 在 Laravel 4 中使用数据库结果填充下拉菜单

mysql - MySQL 可以将字符串转换为表达式吗?

mysql - 匹配除换行符之外的任何字符(mysql 正则表达式)

bash - 如何在 Shell 中解析日期?