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();
}
我猜你執行的結果集多次,使結果集的另一個目的,據我知道你不能重新執行結果集。在循環的結果集內創建新對象 –
您需要在循環內使用不同的語句對象:在語句上執行另一個查詢將關閉從該語句對象創建的所有打開的結果集。您可能還需要禁用自動提交。 –