Java JDBC MySQL 异常:“结果集关闭后不允许操作”并读取网页
Java JDBC MySQL exception: “Operation not allowed after ResultSet closed” with Web Page read
我正在使用 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);
我正在使用 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);