2015-06-21 191 views
0

我試圖讓這個查詢SELECT mac, stop_name from behaviour where mac = ?與使用executeQuery的受影響的記錄數的數量。我怎樣才能得到受影響的行數?獲取受影響的行

代碼:

if (behaviourExist.next()) { 
    PreparedStatement prepared = con 
      .prepareStatement("SELECT mac, stop_name from behaviour where mac = ?"); 
    prepared.setString(1, macD); 
    ResultSet rsBehav = prepared.executeQuery(); 
    ArrayList<String> stopNameList = new ArrayList<String>(); 
    while (rsBehav.next()) { 
     //int numberOfRows = rsBehav.getInt(1); 
     String stNa = rsBehav.getString("stop_name"); 
     if (stNa.equals(nameShortestDistance)) { 
      stopNameList.add(stNa); 

     } 

    } 
} 
+0

http://www.coderanch.com/t/303346/JDBC/databases/find-number-rows-resultset – Pshemo

+0

甲選擇不具有數受影響的行(==添加,更新或刪除)。 –

回答

1

這是一個讀操作,這樣就不會影響任何行。如果你想獲得行返回的數字,那麼你可以做的一個低於

  1. 使用計數器變量,在環加一while (rsBehav.next())

  2. 使用可滾動的ResultSet

    PreparedStatement prepared = con.prepareStatement(
         "SELECT mac, stop_name from behaviour where mac = ?", 
         ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 
    ResultSet rsBehav = prepared.executeQuery(); 
    rsBehav.afterLast(); 
    int numRow = rsBehav.getRow(); 
    rsBehav.beforeFirst(); 
    

這裏numRow行會給你號或者返回行,

0
if (behaviourExist.next()) { 
PreparedStatement prepared = con 
     .prepareStatement("SELECT mac, stop_name from behaviour where mac = ?"); 
prepared.setString(1, macD); 
ResultSet rsBehav = prepared.executeQuery(); 
ArrayList<String> stopNameList = new ArrayList<String>(); 
int numberOfRows = 0; 
while (rsBehav.next()) { 
    ++numberOfRows; 
    String stNa = rsBehav.getString("stop_name"); 
    if (stNa.equals(nameShortestDistance)) { 
     stopNameList.add(stNa); 

    } 
} 
} 

這裏變量numberOfRows會在每次循環運行時遞增,這會給你結果集中的總行數。

0
PreparedStatement prepared = con 
     .prepareStatement(
     "SELECT (SELECT count(*) from where mac = ?)," 
     " mac, stop_name from behaviour where mac = ?"); 
prepared.setString(1, macD); 
prepared.setString(2, macD); 
ResultSet rsBehav = prepared.executeQuery(); 
ArrayList<String> stopNameList = new ArrayList<String>(); 
while (rsBehav.next()) { 
    int numberOfRows = rsBehav.getInt(1); 
    String stNa = rsBehav.getString("stop_name"); 
    // .... 
相關問題