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 Result Set の 1 行目で while ブロックに入るのは 1 回だけです。そのため、1 つの adID のデータのみを書き込みます。「intoQuery」は、すべての rs 結果セット行に対して機能しません。どうすればそれを処理できますか?