2014-05-06 26 views
0

運行此代碼時,出現ResultSet關閉錯誤。是所謂的方法和數據如下:Java JDBC ResultSet關閉

日誌輸出:

[2014-05-05 22:34:09.169 Debug] select count(*) as total from companies 
[2014-05-05 22:34:09.170 Debug] ResultSet closed 

方法:

public static Boolean recordsExist(String string, Connection c) { 
    try { 
     String[] query = string.split("\\*"); 
     String sql = "select count(*) as total" + query[1]; 
     ResultSet resultset = queryDB(sql, c); 
     resultset.next(); 
     int count = resultset.getInt(1); 
     Log.debug(Integer.toString(count)); 
     resultset.close(); 
     if (count > 0) { 
      Log.debug("recordsExist returning true"); 
      return true; 
     } else { 
      Log.debug("recordsExist returning false"); 
      return false; 
     } 

    } catch (Exception e) { 
     Log.debug(e.getMessage()); 
     return false; 
    } 
} 

public static ResultSet queryDB(String sql, Connection c) throws SQLException { 
    Log.debug(sql); 
    Statement s = c.createStatement(); 
    ResultSet resultset = s.executeQuery(sql); 
    s.close(); 
    return resultset; 
} 

指定SQL字符串:

select * from companies 
+2

也爲什麼你關閉結果集在try塊?你應該最後關閉塊 – user3470953

+1

不幸的是,你的異常處理會吞下堆棧跟蹤,這是一種反模式。 –

回答

1

不要關閉聲明。 情景是這樣的第一關結果集然後陳述寫你這樣的代碼:

public static Boolean recordsExist(String string, Connection c) { 
    try { 
     String[] query = string.split("\\*"); 
     String sql = "select count(*) as total" + query[1]; 
     Statement s = c.createStatement(); 
     ResultSet resultset = queryDB(sql, c, s); 
     resultset.next(); 
     int count = resultset.getInt(1); 
     Log.debug(Integer.toString(count)); 
     resultset.close(); 
     s.close(); 
     if (count > 0) { 
      Log.debug("recordsExist returning true"); 
      return true; 
     } else { 
      Log.debug("recordsExist returning false"); 
      return false; 
     } 

    } catch (Exception e) { 
     Log.debug(e.getMessage()); 
     return false; 
    } 
} 

public static ResultSet queryDB(String sql, Connection c, Statement s) throws SQLException { 
    Log.debug(sql); 
    ResultSet resultset = s.executeQuery(sql); 
    return resultset; 
} 
+0

你從來沒有機會關閉這個陳述。 –

+0

我想你應該先清除你。我告訴他不告訴如何編寫代碼的原因。 –

+0

@XingFei它是給你的。查看編輯並清除自己。這是解決方案。 –

3

Java API

注:當一個Statement對象關閉,其當前的ResultSet對象,如果存在的話,也被關閉。

在關閉Statement之前,您需要處理結果集中的數據。沿着這些線應該工作:

public static Boolean recordsExist(String string, Connection c) { 
    try { 
     String[] query = string.split("\\*"); 
     String sql = "select count(*) as total" + query[1]; 
     return queryDB(sql, c); 


    } catch (Exception e) { 
     Log.debug(e.getMessage()); 
     return false; 
    } 
} 

public static boolean queryDB(String sql, Connection c) throws SQLException { 
    Log.debug(sql); 
    Statement s = c.createStatement(); 
    ResultSet resultset = s.executeQuery(sql); 
    boolean result = processResult(resultset); 
    s.close(); 
    return result; 
} 

public static boolean processResult(ResultSet resultset) { 
    resultset.next(); 
    int count = resultset.getInt(1); 
    Log.debug(Integer.toString(count)); 
    resultset.close(); 
    if (count > 0) { 
     Log.debug("recordsExist returning true"); 
     return true; 
    } else { 
     Log.debug("recordsExist returning false"); 
     return false; 
    } 

} 

我也會考慮添加更多的錯誤處理,以確保您不會泄漏資源。例如,如果在處理結果時發生異常,那麼您的語句將不會正確關閉。

1

務必檢查首先設置下一個結果如下圖所示

if(resultset.next()){ 
    int count = resultset.getInt(1); 
} 

我已經發布了一個很好的ConnectionUtil類來管理在整個應用程序的單一類的所有連接。


關閉StatementResultSetConnectionfinally條款到底,以確保所有在任何情況下,關閉如下圖所示的代碼。

Connection conn = null; 
PreparedStatement stmt = null; 
ResultSet rs = null; 
try { 
    ... 
} finally { 
    if (rs != null) { 
     rs.close(); 
    } 
    if (stmt != null) { 
     stmt.close(); 
    } 
    if (conn != null) { 
     conn.close(); 
    } 
} 

注:不要使用Log.debug(e.getMessage());是否有錯誤消息,否則你將在日誌丟失只找出錯誤。

+1

請按照我的建議讓您的代碼更加清晰易懂。在'finally'子句中的一個地方關閉所有的東西。 – Braj