PreparedStatement problem

Hi! I'm having problems while executing the following class.

It seems that the query1.executeQuery is not working.

What could be wrong? I think that this class has no logical errors..

import java.sql.*;

import java.io.*;

class test{

publicstaticvoid main (String[] argv)throws Exception{

try{

StringdriverClassName ="COM.ibm.db2.jdbc.app.DB2Driver";

Stringurl ="jdbc:db2:TS";

Connection dbConnection=null;

Statement st =null;

ResultSet rs=null;

String user ="db2admin";

String pwd ="db2admin";

Class.forName (driverClassName);

dbConnection = DriverManager.getConnection (url,"","");

PreparedStatement query1;

String insertString ="SELECT COUNT(*) FROM KWDS WHERE KWDS.DE=? ;";

query1 = dbConnection.prepareStatement(insertString);

BufferedReader in=new BufferedReader(

new FileReader("Keyword References Keyword.txt"));

BufferedWriter out =new BufferedWriter(

new OutputStreamWriter(

new FileOutputStream(

new File("Keywords with NGD parameters.txt"))));

String linefeed=null;

String temp;

String count1 =null;

String count2 =null;

String count3 =null;

while((linefeed =in.readLine())!=null)

{

String[] linewords = linefeed.split(" :: ");

query1.setString(1,linewords[0]);

rs = query1.executeQuery();

count1 = rs.getString(1);

System.out.println(count1);

query1.setString(1,linewords[1]);

rs = query1.executeQuery();

count2 = rs.getString(1);

temp=null;

temp=linefeed.concat(" :: ").concat(count1).concat(" :: ").concat(count2);

out.write(temp);

out.newLine();

}

in.close();

out.close();

dbConnection.close();

query1.close();

}catch(Exception e){}

}

}

[3279 byte] By [georousa] at [2007-11-26 17:30:54]
# 1
1. Please define not working.If it has errors, what are they? If not what is it doing and what should it be doing instead.2. This is not really a class. It is some code in a main method. I suggest you reorganize things into some classes or at least methods.
zadoka at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0115E Invalid cursor state. SQLSTATE=24000This is an Exception that i get when i execute.
georousa at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

rs = query1.executeQuery();

count2 = rs.getString(1);

You need "rs.next()" between those two lines, so you move the cursor to the first (and only) record of the result set. Otherwise your cursor isn't pointing at a record, and that's an invalid cursor state.

And if your query is "Select count(*) ..." then "insertString" is a horribly bad variable name to represent that query. It doesn't insert anything anywhere.

DrClapa at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
> I think that this class has no logical errors.Just a word of advice: this is not a useful way to think about code that does not do what you want it to do. Wishful thinking just gets in the way of debugging.
DrClapa at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
"SELECT COUNT(*) FROM KWDS WHERE KWDS.DE=? ;"remove ";""SELECT COUNT(*) FROM KWDS WHERE KWDS.DE=?"
masuda1967a at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
> "SELECT COUNT(*) FROM KWDS WHERE KWDS.DE=? ;"> > remove ";"> > "SELECT COUNT(*) FROM KWDS WHERE KWDS.DE=?"That does not matter most of the times and certainly not for that DB2 driver.
aniseeda at 2007-7-8 23:58:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...