(异常)java.sql.SQLException: 未调用 ResultSet.next

当用ResultSet接收来自数据库查询的结果集时,即使结果集只有一条数据,也需要用到resultSet.next()函数移动游标获取数据。

不然会报未调用 ResultSet.next异常

错误写法

try {
    pstm=connection.prepareStatement("select * from account where card_id=?");
    pstm.setInt(1, card_id);
			
    resultSet = pstm.executeQuery();
        account.setCard_id(resultSet.getInt(1));
        account.setUsername(resultSet.getString(2));
        account.setPassword(resultSet.getString(3));
        account.setBalance(resultSet.getDouble(4));
        account.setMobile(resultSet.getString(5));

} 

正确写法

    pstm=connection.prepareStatement("select * from account where card_id=?");
    pstm.setInt(1, card_id);
    resultSet = pstm.executeQuery();
    while (resultSet.next()) {
        account.setCard_id(resultSet.getInt(1));
        account.setUsername(resultSet.getString(2));
        account.setPassword(resultSet.getString(3));
        account.setBalance(resultSet.getDouble(4));
        account.setMobile(resultSet.getString(5));
    }

ResultSet.next 注释信息

public interface ResultSet extends Wrapper, AutoCloseable {

    /**
     * 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;

从此接口的方法注释中第二行和第三行可以看见

A <code>ResultSet</code> cursor is initially positioned
     * before the first row;

游标的初始位置为第一行的前面。

因此不调用next方法就从resultSet中获取数据是不可取的。

这个错误是因为你在调用`java.sql.Connection.createStatement()`方法时,`conn`对象为空。这意味着你没有成功建立数据库连接或者连接已经关闭。你需要确保在调用`createStatement()`方法之前,已经成功建立了数据库连接并且连接对象`conn`不为空。 以下是一个示例代码,演示了如何建立数据库连接并执行SQL查询: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 1. 注册驱动(省略) // 2. 建立数据库连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 3. 创建Statement对象 stmt = conn.createStatement(); // 4. 执行SQL查询 String sql = "SELECT * FROM mytable"; rs = stmt.executeQuery(sql); // 5. 处理查询结果 while (rs.next()) { // 处理每一行数据 // ... } } catch (SQLException e) { e.printStackTrace(); } finally { // 6. 关闭资源 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 请注意,你需要将上述代码中的`"jdbc:mysql://localhost:3306/mydatabase"`替换为你的数据库连接字符串,`"username"`替换为你的数据库用户名,`"password"`替换为你的数据库密码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值