Java JDBC MySQL异常: “Operation not allowed after ResultSet closed” with Web Page read

标签 java mysql jdbc html

我正在使用 MySQL 连接器获取 URL 以在网页上查找值。

我收到上述消息,但不知道为什么。它从 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();
    }

最佳答案

您正在尝试使用已在使用的语句对象进行写入,因为您仍在从该语句对象读取现有结果集。您需要为代码的更新部分创建一个新的语句对象:

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);

关于Java JDBC MySQL异常: “Operation not allowed after ResultSet closed” with Web Page read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346546/

相关文章:

Java:继承类不能在方法参数中转换为父类(super class)

java - 查找运行 web 服务的 android 设备的 IP 地址

mysql - 将 mysql 查询转换为 Eloquent 查询问题

mysql - DB连接池过期了怎么办?

Java Swing 将 JPanel 添加到 JPanel

java - 通过 Mac Java 控制面板更新 jdk 后 El capitan shell java 版本没有改变

php - Mysql插入不在同一个表的记录

php - 如何列出带有相关图像的 wp 帖子?

java - 从 mysql 中以 block 的形式读取一个大的结果集

java - 为什么netbeans看不到我创建的数据库驱动器?