为啥返回 org.h2.jdbc.JdbcSQLException?
Posted
技术标签:
【中文标题】为啥返回 org.h2.jdbc.JdbcSQLException?【英文标题】:Why this return org.h2.jdbc.JdbcSQLException?为什么返回 org.h2.jdbc.JdbcSQLException? 【发布时间】:2019-03-27 12:00:46 【问题描述】:我想设置一个计数器来获取数据库中的记录数:
String sql = " select count(*) as counter from CLIENT ";
conn = DBConnector.getConnection();
try
state = conn.prepareStatement(sql);
result = state.executeQuery();
Counter=result.getInt("counter");
txtUser.setText("user"+(Integer.toString(Counter)));
catch (SQLException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
为什么这段代码会抛出 org.h2.jdbc.JdbcSQLException
?
【问题讨论】:
给我们堆栈跟踪。它将与该行一起打印 e1.printStackTrace(); 【参考方案1】:之后
result = state.executeQuery();
你还需要打电话
result.next();
前进当前行指针。
如果result.next();
返回false
,则没有可用的行。
【讨论】:
非常感谢。我知道数据库中有记录,我猜可能是 result.next(); @S.Iliev 在处理单行结果时,您总是冒着忘记的风险。只需将 ResultSet 视为一个光标,它在开始时不指向任何有效的东西。以上是关于为啥返回 org.h2.jdbc.JdbcSQLException?的主要内容,如果未能解决你的问题,请参考以下文章