gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:37:39 25 4
gpt4 key购买 nike

我的表中有一列包含时间数据类型。我需要打印它的值(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com