Saliha Uzel
Saliha Uzel

Reputation: 151

SQL query doesn't work in while loop - JAVA

I have written a code in Java on NetBeans:

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) {
            }
        }
    }
}

But, it enters the while block only 1 time for the first row of rs Result Set. So, it writes the data only for 1 adID. "intoQuery" does not working for all rs Result Set rows. How can I handle with it?

Upvotes: 3

Views: 2653

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213223

That is because you have executed one more query in a for-loop, inside your while loop: -

for (String distinctCounter : distincts) {                    

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

}

This will result in the old result set to close.

Take a look at : - ResultSet#close documentation, which says that: -

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.

So, the documentation clearly says that re-executing a new query with the same statement will close the ResultSet object.

So, you need to use a different statement instance to create inner query.

Suggestion: -

Ideally your above code throws an SQLException (See ResultSet#next) which you didn't got to know because you swallowed the exception in your catch block.

    catch (SQLException ex) {
    }

Never use an empty catch block. Its of no use, except it prevents your program from stopping at runtime. But it will not do any good.

Upvotes: 8

Related Questions