JDBC 操作与资源管理全解析
1. 操作 ResultSet 时的常见错误
在使用 ResultSet 时,有一些常见的错误需要避免。以下是几个示例:
ResultSet rs = stmt.executeQuery(
"select * from animal where name= 'Not in table'");
rs.next();
rs.getInt(1); // throws SQLException
调用 rs.next() 会返回 false ,因为没有匹配的结果。之后调用 getInt(1) 会抛出 SQLException ,因为结果集的游标没有指向有效的位置。
ResultSet rs = stmt.executeQuery("select count(*) from animal");
rs.getInt(1); // throws SQLException
没有调用 rs.next() 是一个问题。结果集的游标仍然指向第一行之前的位置,所以 getInt(1) 没有可指向的内容。
ResultSet rs = stmt.executeQuery("select co
超级会员免费看
订阅专栏 解锁全文
9097

被折叠的 条评论
为什么被折叠?



