Ava
Ava

Reputation: 6053

Java program getting terminated before Resultset.next()

My program is get terminated just after printing hello in getImportance beg. Why so? My MAIL table has values.

Connection connection = connectToDatabase();
            Statement stmt = connection.createStatement();
            ResultSet mailset = stmt.executeQuery("SELECT SUBJECT from MAIL");
            ResultSet keywordset = stmt.executeQuery("SELECT SKEYWORD FROM KEYWORD");
            System.out.println("hello in getImportance beg"); 
            while(mailset.next())
            {               
                System.out.println("hello in first while"); 

                while(keywordset.next())
                {
                    System.out.println("hello in second while"); 
                    if(mailset.getString("SUBJECT").equals(keywordset.getString("SKEYWORD")));
                    {
                        System.out.println("hello in if"); 
                        stmt.executeUpdate("UPDATE KEYWORD SET IMPORTANCE = IMPORTANCE'" + 1 + "'");
                        stmt.executeUpdate("COMMIT");
                    }
                }
            }
            mailset.close();
            keywordset.close();
            connection.close();

What am I missing?

Thanks!


Thanks everyone for the replies. Defining the second ResultSet after first while did the trick.

Upvotes: 0

Views: 2074

Answers (3)

Kayser
Kayser

Reputation: 6694

The problem may be the mailset. it delivers probably no result. You can check it with System.out.println(mailset.next()); before the first loop, or debug it.

Another issue on the second resultset. As I understand: you try to iterate each time over keywordset. Then you need to go to the beginning of the result set after the second while loop (while(keywordset.next()) using keywordset.beforeFirst() .

Upvotes: 0

Cameron Skinner
Cameron Skinner

Reputation: 54456

The problem is probably that you are trying to obtain two ResultSets from a single Statement.

From the java.sql.Statement documentation: "By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists."

Upvotes: 1

Your while loop is obviously not running.

Check the result of mailset.next()

Upvotes: 3

Related Questions