2017-06-03 81 views
0

我正在使用MySQL连接器来获取URL以在网页上查找值。Java JDBC MySQL异常:使用Web页读取“ResultSet关闭后不允许的操作”

我收到上面的消息,我不知道为什么。它从rs1插入第一条记录,但我不确定它为什么会关闭它。

下面是我的代码

String strSQL = "SELECT * FROM element_info;"; 
    String sElementID = ""; 
    String sSymbol = ""; 
    URL urlChartLink; 
    URLConnection urlconn; 
    String sChartLink = ""; 
    String sCurrentPrice = ""; 
    String FindValue = "last_last"; 

    try { 

     Class.forName(driver).newInstance(); 
     Connection mysqlconn = DriverManager.getConnection(url + dbName, userName, password); 
     Statement st1 = mysqlconn.createStatement(); 
     ResultSet rs1 = st1.executeQuery(strSQL); 

     while (rs1.next()) { 
      // Get all of the elements 
      // Retrieve the ElementID 
      sElementID = rs1.getString(1); 
      // Retrieve the Symbol 
      sSymbol = rs1.getString(2); 
      // Retrieve the Chartlink 
      sChartLink = rs1.getString(3); 
      if (sChartLink == "") { 
       break; 
      } 

      try { 

       urlChartLink = new URL(sChartLink); 
       urlconn = urlChartLink.openConnection(); 
       urlconn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); 
       BufferedReader in = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), "UTF-8")); 
       String currentLine; 

       while ((currentLine = in.readLine()) != null) { 
        // See if the value is on this record 
        int pos1 = currentLine.indexOf(FindValue); 
        int pos2 = currentLine.indexOf("</span>"); 
        // pos1 = 66 
        if (pos1 > 0) { 
         pos1 = pos1 + 21; 
         pos2 = pos2 - 1; 
         // System.out.print("pos1 = " + pos1 + "\n"); 
         // System.out.print("pos2 = " + pos2 + "\n"); 

         sCurrentPrice = currentLine.substring(pos1, pos2); 
         // System.out.print("sCurrentPrice = " + sCurrentPrice + "\n"); 

         // Import into the marketprices 
         strSQL = "INSERT INTO marketprices" 
           + "(ElementID,Symbol,PullDatetime,Price) VALUES (" + "'" + sElementID + "','" 
           + sSymbol + "','" + sToday + "','" + sCurrentPrice + "')"; 

         int val = st1.executeUpdate(strSQL); 

         if (val == 1) 
          System.out.print("Successfully inserted from " + sChartLink + "\n"); 
         break; 
        } 
       }     
       in.close(); 
      } catch (IOException e) { 
       System.out.print("Error getting ChartLink website: " + e.getMessage() + "\n"); 
       break; 
      } 
     }   
    } catch (Exception e) { 
     System.out.print("Error: " + e.getMessage() + "\n"); 
     e.printStackTrace(); 
    } 
+1

我猜你执行的结果集多次,使结果集的另一个目的,据我知道你不能重新执行结果集。在循环的结果集内创建新对象 –

+0

您需要在循环内使用不同的语句对象:在语句上执行另一个查询将关闭从该语句对象创建的所有打开的结果集。您可能还需要禁用自动提交。 –

回答

0

您正在尝试与语句对象,它是已在使用,你还在读书从Statement对象现有的结果集写的。您需要创建一个新的Statement对象为你的代码的更新部分:

strSQL = "INSERT INTO marketprices"+ "(ElementID,Symbol,PullDatetime,Price) VALUES (" + "'" + sElementID + "','"+ sSymbol + "','" + sToday + "','" + sCurrentPrice + "')"; 
Connection mysqlconn2 = DriverManager.getConnection(url + dbName, userName, password); 
Statement st2 = mysqlconn.createStatement(); 
int val = st2.executeUpdate(strSQL); 
+0

谢谢!就是这样!一旦我做出改变,它就像一个魅力! – EddiRae

相关问题