SQLException: Before start of result set

21 篇文章 0 订阅
5 篇文章 0 订阅

背景

用惯了mybatis,今天用原生的jdbc获取链接,查询数据时,取数据报错。问题代码片段

Connection conn = getConnection();
PreparedStatement preparedStatement = conn.prepareStatement("SELECT max(version) as cur_version FROM test_version ");
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet.getInt("cur_version");

报错信息如下:

java.sql.SQLException: Before start of result set
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:484) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1283) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:786) ~[mysql-connector-java-8.0.17.jar:8.0.17]
	at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:807) ~[mysql-connector-java-8.0.17.jar:8.0.17]

问题

通过报错定位,可知报错位置为 resultSet.getInt(“cur_version”) 获取数据异常。

解决

查询资料得知,获取数据前需要调用 resultSet.next() 方法,然后才能取数据。代码调整为如下即可:

Connection conn = getConnection();
PreparedStatement preparedStatement = conn.prepareStatement("SELECT max(version) as cur_version FROM test_version ");
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet.next() ? resultSet.getInt("cur_version") : -1;

总结

查看 java.sql.ResultSet#next 方法可知,ResultSet 指针的初始位置位于第一行之前;第一次调用next()方法将会把第一行设置为当前行;
调用 next() 方法后才能开始获取数据。这种做法的好处在于可以方便地配合while进行遍历,避免取不到第一行数据。

源码解释如下:

    /**
     * Moves the cursor forward one row from its current position.
     * A <code>ResultSet</code> cursor is initially positioned
     * before the first row; the first call to the method
     * <code>next</code> makes the first row the current row; the
     * second call makes the second row the current row, and so on.
     * <p>
     * When a call to the <code>next</code> method returns <code>false</code>,
     * the cursor is positioned after the last row. Any
     * invocation of a <code>ResultSet</code> method which requires a
     * current row will result in a <code>SQLException</code> being thrown.
     *  If the result set type is <code>TYPE_FORWARD_ONLY</code>, it is vendor specified
     * whether their JDBC driver implementation will return <code>false</code> or
     *  throw an <code>SQLException</code> on a
     * subsequent call to <code>next</code>.
     *
     * <P>If an input stream is open for the current row, a call
     * to the method <code>next</code> will
     * implicitly close it. A <code>ResultSet</code> object's
     * warning chain is cleared when a new row is read.
     *
     * @return <code>true</code> if the new current row is valid;
     * <code>false</code> if there are no more rows
     * @exception SQLException if a database access error occurs or this method is
     *            called on a closed result set
     */
    boolean next() throws SQLException;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值