3

我在 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 结果集行。我该如何处理?

4

1 回答 1

8

那是因为您在 while 循环内的 for 循环中又执行了一个查询:-

for (String distinctCounter : distincts) {                    

    // Create a new statement to execute query in while loop                
    Statement stmt1 = conn.createStatement();  
    stmt1.execute(intoQuery);

}

这将导致旧result set的关闭。

看一下:-ResultSet#close文档,其中说:-

当 Statement 对象关闭、重新执行或用于从多个结果序列中检索下一个结果时,生成它的 Statement 对象会自动关闭 ResultSet 对象。

因此,文档清楚地表明,使用相同的语句重新执行新查询将关闭该ResultSet对象。

因此,您需要使用不同的statement实例来创建内部查询。

建议: -

理想情况下,您上面的代码会抛出一个您不知道的SQLException(See ),因为您在块中吞下了异常。ResultSet#nextcatch

    catch (SQLException ex) {
    }

永远不要使用emptycatch 块。它没有用,除了它可以防止您的程序在运行时停止。但这不会有任何好处。

于 2012-10-16T13:07:21.257 回答