2014-02-12 50 views
4

我不能做進一步的過程,因爲我在運行此代碼時關閉了ResultSet。我在sqlite數據庫表的第15行後面添加值,我想查找15行的平均值,並且應該存儲在ArrayList中。java.sql.SQLException:ResultSet關閉

下面是代碼: -

try { 
    Class.forName("org.sqlite.JDBC"); 
    c = DriverManager.getConnection("jdbc:sqlite:test.db"); 
    c.setAutoCommit(false); 
    System.out.println("Opened database successfully3"); 
    stmt = c.createStatement(); 
    rs = stmt 
      .executeQuery("SELECT TIME,BID,ASK FROM '" + title + "' ;"); 
    System.out.println("Opened database successfully 20"); 
    while (rs.next()) { 
     i++; 
     System.out.println(i); 
     if (i > 15) { 
      System.out.println("i am in"); 
      List<String> stocklist = new ArrayList<String>(); 
      String main = rs.getString(1); 
      System.out.println(rs.getString(1)); 
      String s[] = main.split(" ", 2); 
      String date = s[0]; 
      String time = s[1]; 
      stocklist.add(date); 
      stocklist.add(time); 
      stocklist.add(df.format(rs.getFloat("BID"))); 
      stocklist.add(df.format(rs.getFloat("ASK"))); 
      rs1 = stmt 
        .executeQuery("SELECT ASK FROM '" + title + "' ;"); 
      int j = 1; 
      while (rs1.next()) { 
       if (j < i) { 
        System.out.println(rs1.getFloat("ASK")); 
        avg = avg + rs1.getFloat("ASK"); 
       } 
       j++; 
      } 
      rs1.close(); 

      System.out.println("i am out"); 
      avg = avg/15; 
      changepercent = ((rs.getFloat("ASK") - avg)/avg) * 100; 
      stocklist.add(df.format(changepercent)); 
      stocklist.add("ETE"); 
      stocklist.add("HUN"); 
      stocklist.add("ALU"); 
      stocklist.add("ETE"); 
      stocklist.add("HUN"); 
      stocklist.add("ALU"); 
      stocklist.add("ETE"); 
      stocklist.add("HUN"); 
      hntm.addRow(stocklist); 
     } 
    } 
    rs.close(); 
    stmt.close(); 
    c.close(); 
} catch (Exception e) { 
    System.err.println(e.getClass().getName() + ": " + e.getMessage()); 
    System.exit(0); 
} 
+0

更換rs1 = stmt.executeQuery("SELECT ASK FROM '" + title + "' ;");嘗試創建內部的RS1 –

+0

非常感謝,它的工作... WOW – Laxmeena

回答

8

你不應該重用的聲明。當你創建一個新的查詢時,你需要使用一個新的Statement對象。與rs1=c.createStatement().executeQuery("SELECT ASK FROM '" + title + "' ;");

+0

感謝名單環路另一份聲明中,我已經按照你的工作做了.. – Laxmeena

+0

謝謝,我不知道這工作 –