Running the following code gives:
String 0 is blah
Exception in thread "main" java.lang.NullPointerException
at org.postgresql.jdbc2.ResultSet.next(ResultSet.java:113)
at PostgresTest.test1(PostgresTest.java:33)
at PostgresTest.main(PostgresTest.java:50)
Commenting out the executeUpdate() inside the for loop solves the problem. I think a
ResultSet that is closed should throw an IllegalStateException, not just let a
NullPointerException be thrown.
/**
* PostresTest.java
*
*
* Created: Thu Apr 25 15:51:47 2002
*
* @author <a href="mailto:jks@selectacast.net">Joseph Shraibman</a>
* @version
*/
import java.sql.*;
public class PostgresTest{
private static void test1(String usr, String pwd) throws SQLException,
ClassNotFoundException{
int fetch_size = 50;
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection("jdbc:postgresql:playpen", usr, pwd);
Statement st = db.createStatement();
st.executeUpdate("BEGIN;");
st.executeUpdate("CREATE table ptest1 (a int, b text);");
try{
st.executeUpdate("INSERT into ptest1 VALUES(1, 'blah');");
st.executeUpdate("DECLARE cname CURSOR FOR SELECT b FROM ptest1 GROUP BY b");
st.executeUpdate("CREATE TEMP TABLE tqt (id int, bstr text);");
while(true){
ResultSet rs = st.executeQuery("FETCH "+fetch_size+" FROM cname;");
int i = 0;
for ( ; rs.next() ; i++ ) {
String b = rs.getString(1);
System.out.println("String "+i+" is "+b);
st.executeUpdate("INSERT INTO tqt SELECT a FROM ptest1 where b =
'"+b+"';");
//st.executeUpdate("DELETE from tqt WHERE bstr = '"+b+"';");
}
if (i < fetch_size)
break;
}
st.executeUpdate("END;");
st.executeUpdate("DROP TABLE tqt;");
}finally{
st.executeUpdate("DROP table ptest1;");
st.executeUpdate("ABORT;");
}
}
public static void main(String[] args)throws Exception{
test1(args[0], args[1]);
}
} // PostresTest