gpt4 book ai didi

java - 如何在jsp中显示SQL的结果?

转载 作者:行者123 更新时间:2023-11-30 07:46:27 25 4
gpt4 key购买 nike

下面是jsp中的SQL语句。正如你所看到的,我想显示我的 SQL 结果,但没有这样做。我相信我的方法是错误的。请帮助我。

Connection connection = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
Statement statement = connection.createStatement();
String query ="SELECT COUNT(CATEGORY) AS COUNTER FROM CLASSIFY WHERE CATEGORY='REMEMBER'";
ResultSet rst =statement.executeQuery(query);

String counteRemember= rst.getString("COUNT");
out.println(counteRemember);
}
catch(ClassNotFoundException e)
{
out.print("error1");
}
catch (SQLException e)
{
out.print("error2");
}

最佳答案

您调用了错误的列(COUNT 是您正在调用的 sql 函数,但不是结果列):

String counteRemember= rst.getString("COUNT");

将其更改为:

if(rst.next()) {
String counteRemember= rst.getString("COUNTER");

}

或者:

String counteRemember= rst.getString(1);

关于java - 如何在jsp中显示SQL的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33867779/

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