2010-10-22 92 views
0

我想用Java連接到MySQL數據庫。我想從數據庫中的兩行中獲取兩行的所有條目,並將它們放入ExpertScore類中。然後,我想將新創建的ExpertScore對象放入ExpertScore對象的數組中。在此之後,我想通過一個將ExpertScore對象數組作爲輸入的方法來運行它們。但是,我運行代碼並得到這個錯誤。一些調試表明,我認爲問題是由結果對象的計數造成的。連接Java和MySQL的代碼的JDBC結果集

java.sql.SQLException: Before start of result set 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929) 
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841) 
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2672) 
at ZScoreCalc.main(ZScoreCalc.java:106) 

這是我的代碼導致錯誤:

public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ 
    System.out.println("MySQL Connect Example."); 
    Connection conn = null; 
    String url = "jdbc:mysql:*"; // server address 
    String dbName = "QA"; //table name 
    String driver = "com.mysql.jdbc.Driver"; // jdbc driver 
    String userName = "*"; //username 
    String password = "*"; //password 
    Class.forName(driver).newInstance(); 
    try { 
     conn = DriverManager.getConnection(url+dbName,userName,password); 
     System.out.println("Connected to the database"); 

     for(int j=1; j<11; j++){ 
     String query = "select * from CONSUMER_EXPERT_SCORE where CONSUMER_EXPERT_ID="+j; 
     String queryforcount = "select count(*) from CONSUMER_EXPERT_SCORE where CONSUMER_EXPERT_ID="+j; 
     PreparedStatement pscount = conn.prepareStatement(queryforcount); 
     ResultSet resultcount = pscount.executeQuery(); 
     int count = resultcount.getInt(0); 
     PreparedStatement ps = conn.prepareStatement(query); 
     ResultSet result = ps.executeQuery(); 
     int i=0; 
     ExpertScore[] allUsers=new ExpertScore[count]; 
     while(result.next()){ 
     double expert=result.getDouble(3); 
     int id=result.getInt(2); 
     ExpertScore current=new ExpertScore(id, j, expert); 
     allUsers[i]=current; 
     i++; 
     } 
     ZScoreCalc scrCalc = new ZScoreCalc(); 
     scrCalc.Z_Calc(allUsers); 
     scrCalc.print(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
} 

有誰知道這是怎麼回事?對不起,我是編程新手,尤其是JDBC新手。

+1

我強烈建議你修改了你的IP /用戶名/密碼來自你的問題的信息。哦,並改變你的mysql密碼。 – Bill 2010-10-22 22:47:23

+0

@Bill:好主意,但爲時已晚,真的 – 2010-10-22 22:55:37

+0

您的堆棧跟蹤表示錯誤發生在第106行的getInt()調用上。在代碼中有多個對getInt()的調用。你認爲我們不知道106線在哪裏?嘗試在執行下一個查詢之前調用pscount.close()。驅動程序應該自動爲你做,但是使用MySql,任何事情都可能發生。另外,最好的做法是使用「?」在你的SQL中代替「+ j」,然後調用pscount.setInt(1,j)來建立值 – 2010-10-22 23:00:40

回答

3

你需要調用resultcount.getInt(0);

之前調用resultcount.next()它是很好的做法,關閉你的結果集的最後條款。這裏通常是你想要使用的結構。

//in general use a structure like this: 
ResultSet rs = null; 
PreparedStatemetn pStmt = null; 
try { 

pStmt = conn.prepareStatement("Select * from foo"); 
rs = pStmt.executeQuery(); 
while (rs.next()) { 
    String data = rs.getString(1); 
} 
} 
catch(Exception e){ 
//handle exception 
} 
finally { 
    try { 
    if (rs != null) rs.close(); 
    if (pStmt != null) pStmt.close(); 
    } 
    catch (Exception ignored) { 
    } 
} 

}

專家提示:對於常見的「關閉此資源的忽略例外」模式,建立實用方法:

class DbUtil { 
    public void closeQuietly(ResultSet rs) { 
     try { 
     if (rs != null) rs.close(); 
     } 
     catch (Exception ignored) {} 
    } 

    public void closeQuietly(Statement stmt) { 
     try { 
     if (stmt != null) stmt.close(); 
     } 
     catch (Exception ignored) {} 
    } 
}