2015-03-02 88 views
0

我有這個錯誤,我不明白,有人可以告訴我我做錯了什麼嗎?java.sql.SQLException:列位置'1'超出範圍

java.sql.SQLException:列位置'1'超出範圍。此ResultSet的列數爲'0'。

public Object[][] searchFunction(String searchWord, String loginId) { 
    Object[][] data = new Object[5][6]; 
    String[] tableName = {"subject", "sender", "recipient", "label", "message"}; 
    int nr = 0, i = 0, q = 1; 
    boolean hasRows = false; 
    for(int j = 0; j<5; j++) { 
     try {  
      PreparedStatement prepareStatement = con.prepareStatement("SELECT * FROM messages WHERE sender = ? AND " + tableName[j] + " LIKE '%Done%' ORDER BY id ASC");  
      prepareStatement.setString(1, loginId);  
      rs = prepareStatement.executeQuery(); 
      while (rs.next()) { 
       hasRows = true; 
       boolean check = true; 
       int id = rs.getInt("id"); 
       int priority = rs.getInt("priority"); 
       String recipient = rs.getString("recipient"); 
       String labelDb = rs.getString("label"); 
       String subject = rs.getString("subject");  
       String date = rs.getString("date");  
       String stars = setPriorityStars(priority); 

       for(int k=0; k<i+1; k++) { 
        Object idObj = (Integer) id; 
        if(data[k][0] == idObj) check = false; 
       } 

       if(check == true) { 
         data[i][0] = q; 
         data[i][1] = stars; 
         data[i][2] = recipient; 
         data[i][3] = labelDb; 
         data[i][4] = subject; 
         data[i][5] = date; 
         i++; 
         nr++; 
         q++; 
        } 

      } 
      if(hasRows == false) { 
      data = getInbox(loginId); 
      JOptionPane.showMessageDialog(null, "There is no fields that includes " + searchWord); 
      break; 
      } 
     } catch(Exception ex) { 
      System.out.println(ex); 
     } 
    } 
    System.out.println("Found " + nr + " results"); 
    return data; 
} 

回答

0

它是在這裏

prepareStatement.setString(1, loginId); 

在你查詢你不必?設置這個值。改變你的查詢尊重loginid或刪除這條線,如果登錄ID沒有任何意義

+0

我正在做實驗,忘了說...但不是因爲那個.. – 2015-03-02 17:24:48

+0

他不喜歡LIKE語句... – 2015-03-02 17:31:16

0

它可能告訴你,loginId字符串中的值比名爲sender的數據庫列更長,您正在比較它在where子句。

此外,您編寫的代碼會泄漏大量內存,因爲在您的循環的下一次迭代中創建新代碼之前未關閉PreparedStatement。

相關問題