2011-05-18 60 views
0

我的代碼:JDBC問題問心無愧選取查詢

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 [email protected] 
    select QuotaUsed from BmgCPUTotalUsage where CPUserID='msdp' and MessageTypeID='SMS' 
    [email protected] 
    java.sql.SQLException: Before start of result set. 

可有人請說我如何刪除此exception.and從table.where獲取QUOTAUSED QUOTAUSED是一個整數。

+0

你有沒有經歷過它是如何接受一個答案?現在就試試。它確實有奇效。相信我。 – 2011-05-18 09:10:42

回答

8

你真的需要做JDBC Tutorial的多讀書,但簡要地說,試試這個:

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