我在NetBeans上用Java编写了一个代码:
package helloworld;
import java.sql.*;
import java.io.*;
import java.lang.Object;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.lucene.queryParser.ParseException;
public class HelloWorld {
public static void main(String[] args) throws IOException, ParseException {
// TODO code application logic here
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/kdd";
String user = "root";
String password = "Pass1234";
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sqlQuery = "Select * from ad_contents";
rs = stmt.executeQuery(sqlQuery);
while (rs.next()) { //(rs.next())
int adID = rs.getInt(1);
String keywordTokens = rs.getString(2);
String titleTokens = rs.getString(3);
String descriptionTokens = rs.getString(4);
List keywordList = Arrays.asList(keywordTokens.split("\\|", -1));
List titleList = Arrays.asList(titleTokens.split("\\|", -1));
List descriptionList = Arrays.asList(descriptionTokens.split("\\|", -1));
Set distincts = new HashSet();
distincts.addAll(keywordList);
distincts.addAll(titleList);
distincts.addAll(descriptionList);
int keywordHit = 0, titleHit = 0, descriptionHit = 0;
for (String distinctCounter : distincts) {
keywordHit = CollectionUtils.cardinality(distinctCounter, keywordList);
titleHit = CollectionUtils.cardinality(distinctCounter, titleList);
descriptionHit = CollectionUtils.cardinality(distinctCounter, descriptionList);
String intoQuery = "insert into ad_word_counts values (" + adID + "," + distinctCounter + "," + keywordHit + "," + titleHit + "," + descriptionHit + ")";
stmt.execute(intoQuery);
//node myNode= new node(adID, Integer.parseInt(distinctCounter) , keywordHit, titleHit, descriptionHit);
}
}
} catch (SQLException ex) {
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
}
}
}
}
但是,对于rs Result Set的第一行,它只进入while块一次.因此,它仅为1个adID写入数据. “intoQuery”不适用于所有rs Result Set行.我怎么处理它?
解决方法:
那是因为你在while循环中的for循环中再执行了一次查询: –
for (String distinctCounter : distincts) {
// Create a new statement to execute query in while loop
Statement stmt1 = conn.createStatement();
stmt1.execute(intoQuery);
}
这将导致旧结果集关闭.
A ResultSet object is automatically closed by the Statement object
that generated it when that Statement object is closed, re-executed,
or is used to retrieve the next result from a sequence of multiple
results.
因此,文档清楚地表明,使用相同的语句重新执行新查询将关闭ResultSet对象.
因此,您需要使用不同的语句实例来创建内部查询.
建议: –
理想情况下,您的上面的代码会抛出一个SQLException(参见ResultSet#next),您不会知道它,因为您吞下了catch块中的异常.
catch (SQLException ex) {
}
切勿使用空挡块.它没用,除了它阻止你的程序在运行时停止.但它不会有任何好处.
标签:java,while-loop,mysql,netbeans,resultset
来源: https://codeday.me/bug/20190725/1537096.html