gpt4 book ai didi

java ojdbc 6 getInt() 按列号返回错误值

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:57 26 4
gpt4 key购买 nike

我从 ojdbc getInt() 按列号收到了一个非常有趣的错误。

ps = conn.prepareStatement("select count(*) from tableA");
rs = ps.executeQuery();
int x;
if (rs.next())
x = rs.getInt(1);

ps = conn.prepareStatement("select count(*) as someVariable from tableA");
rs = ps.executeQuery();
int y;
if (rs.next())
y = rs.getInt(1);

x 和 y 的值不同。 y 是我期望的正确数字。

我正在使用 java 7 和 ojdbc6.jar (11.2.0.3.0)

谢谢。

最佳答案

ps = conn.prepareStatement("select count(*) as someVariable from tableA");
ps = conn.prepareStatement("select count(*) as result from tableA");
rs = ps.executeQuery();
int x;
if (rs.next())
x = rs.getInt("result");

ps = conn.prepareStatement("select count(*) as someVariable from tableA");
rs = ps.executeQuery();
int y;
if (rs.next())
y = rs.getInt("someVariable");

你是不是忘记了 rs.close()、conn.close()、statement.close()?最好使用尝试资源,而不是仅仅尝试。

try(Connection con = DriverManager.getConnection("DBpath","usr","pswd")) {
//doSomething
}

在这种情况下,您不需要关闭它们,它们会自动关闭。注意:您只能使用可关闭的对象来执行此操作。

关于java ojdbc 6 getInt() 按列号返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35205634/

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