Thread: next() and PreparedStatement
Hi! We are developing a project using PostgreSQL 7.1.2 and JDBC drivers to exchange information with the database. Here is a piece of code to explain our problem: --------------------------- PreparedStatement pstmt; Connection connection; ResultSet rs = null; String query = new String( "SELECT count(*) FROM foo_table WHERE foo_code=?;" ); pstmt = connection.prepareStatement( query ); pstmt.setString( 1, foo ); rs = pstmt.executeQuery( query ); pstmt.close(); return rs.next(); -------------------------- the next() method will cause a java.lang.NullPointerException when the ResultSet comes from a PreparedStatement!!! Is this problem originated by the JDBC driver? We downloaded it from jdbc.fastcrypt.com but this error occurs again and again and in jdbc.postgresql.org we see 7.0.x drivers available but none 7.1 nor 7.1.2 exists... We'd appreciate any help you could give us.If your address is not the correct place to ask this questions please tell us. Thanks in advance. _________________________________________________________________ Descargue GRATUITAMENTE MSN Explorer en http://explorer.msn.es/intl.asp
i believe that when you close the stmt, the rs is automatically closed. you have to leave the stmt open until you're done with the rs. -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Mano lito Sent: Tuesday, August 28, 2001 7:14 AM To: pgsql-jdbc@postgresql.org Subject: [JDBC] next() and PreparedStatement Hi! We are developing a project using PostgreSQL 7.1.2 and JDBC drivers to exchange information with the database. Here is a piece of code to explain our problem: --------------------------- PreparedStatement pstmt; Connection connection; ResultSet rs = null; String query = new String( "SELECT count(*) FROM foo_table WHERE foo_code=?;" ); pstmt = connection.prepareStatement( query ); pstmt.setString( 1, foo ); rs = pstmt.executeQuery( query ); pstmt.close(); return rs.next(); -------------------------- the next() method will cause a java.lang.NullPointerException when the ResultSet comes from a PreparedStatement!!! Is this problem originated by the JDBC driver? We downloaded it from jdbc.fastcrypt.com but this error occurs again and again and in jdbc.postgresql.org we see 7.0.x drivers available but none 7.1 nor 7.1.2 exists... We'd appreciate any help you could give us.If your address is not the correct place to ask this questions please tell us. Thanks in advance. _________________________________________________________________ Descargue GRATUITAMENTE MSN Explorer en http://explorer.msn.es/intl.asp ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> i believe that when you close the stmt, the rs is automatically > closed. you have to leave the stmt open until you're done with the > rs. Exactly. If you read the Java documentation for interface java.sql.Statement, method close(), it tells you the Resultset, if any, also is closed on that call. PreparedStatement extends Statement, CallableStatement extends PreparedStatement. It's important to notice that the Postgres implementation of Resultset et al is meant to work as described in the standard Java documentation; that's the whole point of the interfaces. Thus a lot of these questions can be answered by carefully reading through the standard Java documentation. Yours, ///Jens Carlberg
Hi Mano, No the problem is not with jdbc. I do see a couple of things though 1) the semi-colon isn't necessary, but shouldn't break the code? try removing it. 2) returning the row that way looks a little dangerous. It is possible that the statement associated with the row would be gone after returning. dave On Tue, 2001-08-28 at 07:13, Mano lito wrote: > > > Hi! > > We are developing a project using PostgreSQL 7.1.2 and JDBC drivers to > exchange information with the database. Here is a piece of code to explain > our problem: > > --------------------------- > PreparedStatement pstmt; > Connection connection; > ResultSet rs = null; > String query = new String( "SELECT count(*) FROM foo_table WHERE > foo_code=?;" ); > pstmt = connection.prepareStatement( query ); > pstmt.setString( 1, foo ); > rs = pstmt.executeQuery( query ); > > pstmt.close(); > > return rs.next(); > -------------------------- > > the next() method will cause a java.lang.NullPointerException > when the ResultSet comes from a PreparedStatement!!! Is this problem > originated by the JDBC driver? We downloaded it from jdbc.fastcrypt.com > but this error occurs again and again and in jdbc.postgresql.org we see > 7.0.x drivers available but none 7.1 nor 7.1.2 exists... > > We'd appreciate any help you could give us.If your address is not the > correct place to ask this questions please tell us. > > Thanks in advance. > > > > > > _________________________________________________________________ > Descargue GRATUITAMENTE MSN Explorer en http://explorer.msn.es/intl.asp > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org > >