This question already has answers here:
ResultSet exception - before start of result set
                                
                                    (6个答案)
                                
                        
                2年前关闭。
            
        

我的代码:

con = jdbc.connect();
        System.out.println("status of con"+con);
String query = " select * from BmgCPUTotalUsage where CPUserID='"+CPUserID+"' and MessageTypeID='"+MessageTypeID+"'";
        System.out.println(query);
        PreparedStatement statement = con.prepareStatement(query);
    ResultSet result = statement.executeQuery();
    System.out.println(result);
    int QuotaUsed=result.getInt("QuotaUsed");


输出:

   status of concom.mysql.jdbc.JDBC4Connection@182600f
     select QuotaUsed from BmgCPUTotalUsage where CPUserID='msdp' and MessageTypeID='SMS'
    com.mysql.jdbc.JDBC4ResultSet@16e1dd8
    java.sql.SQLException: Before start of result set.


有人可以告诉我如何删除此异常。并从表中获取QuotaUsed。其中QuotaUsed是整数。

最佳答案

您确实需要对JDBC Tutorial进行更多阅读,但是请简要尝试一下:

ResultSet resultSet = statement.executeQuery();
if (resultSet.next() {
    int quotaUsed = resultSet.getInt("QuotaUsed");
}

10-05 18:25