我在 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<String> keywordList = Arrays.asList(keywordTokens.split("\\|", -1));
List<String> titleList = Arrays.asList(titleTokens.split("\\|", -1));
List<String> descriptionList = Arrays.asList(descriptionTokens.split("\\|", -1));
Set<String> distincts = new HashSet<String>();
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 结果集的第一行,它只进入 while 块 1 次。因此,它只为 1 个 adID 写入数据。“intoQuery”不适用于所有 rs 结果集行。我该如何处理?