Thread: Connection.setBytes()
I want to set an array of bytes to a prepared statement, but it throws a null pointer exception. Here is my code: String description = new String(".........4000 Bytes......."); PreparedStatement pstmt = con.prepareStatement("INSERTINTO mytable (str) VALUES (?)"); pstmt.setBytes(1,description.getBytes()); //Line 65, throws NullPointerException here pstmt.executeUpdate(); pstmt.close(); Exception in thread "main" java.lang.NullPointerExceptionat org.postgresql.Connection.ExecSQL(Connection.java:312)at org.postgresql.jdbc2.Statement.execute(Statement.java:273)at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)at org.postgresql.largeobject.LargeObjectManager.<init>(LargeObjectManager.java:106) at org.postgresql.Connection.getLargeObjectAPI(Connection.java:561)at org.postgresql.jdbc2.PreparedStatement.setBytes(PreparedStatement.java:297) ==== >at PsqlConsole.prompt(PsqlConsole.java:65) <====at PsqlConsole.main(PsqlConsole.java:87) It throws it when I call setBytes(), I don't know what the problem is. I got the source of setBytes and put it in my code where I have setBytes(). It was throwing a null pointer when it was calling getLargeObjectAPI(). Has anyone else had this error? Thanks Matt Fair
On Tue, 18 Jul 2000, Matt Fair wrote: > > I want to set an array of bytes to a prepared statement, but it throws a > null pointer exception. Did you turn off autocommit? BLOB code must run in the context of a transaction. I believe autocommit defaults to true, so you must explicitly set it to false and call commit at the end of your transactions. Joachim
Yes, I used con.setAutoCommit(false); Joachim Achtzehnter wrote: > On Tue, 18 Jul 2000, Matt Fair wrote: > > > > I want to set an array of bytes to a prepared statement, but it throws a > > null pointer exception. > > Did you turn off autocommit? BLOB code must run in the context of a > transaction. I believe autocommit defaults to true, so you must explicitly > set it to false and call commit at the end of your transactions. > > Joachim
Here is the code I am using: import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.postgresql.largeobject.*; import org.postgresql.util.*; class PsqlConsole { private Connection con; private Statement stmt; private PreparedStatement ps; public PsqlConsole(String url) { try{ //load the db driver Class.forName("org.postgresql.Driver"); //get a connection to the database System.out.println("jdbc:postgresql://"+url); con = DriverManager.getConnection("jdbc:postgresql://"+url, "matt", "matt"); con.setAutoCommit(false); } catch(ClassNotFoundException e) { System.out.println("Could not load the following database driver:" + e.getMessage()); } catch(SQLException e) { System.out.println("SQLException caught: " +e.getMessage()); } finally { //always close the database try { if(con != null)con.close(); } catch(SQLException ignored) { } } } public void prompt() throws SQLException, IOException{ String description = new String(".........4000 Bytes......."); PreparedStatement pstmt = con.prepareStatement("INSERTINTO mytable (str) VALUES (?)"); pstmt.setBytes(1,description.getBytes()); //Line 49 pstmt.executeUpdate(); pstmt.close(); } public static void main(String[] args) { if(args.length > 0){ String url = args[0]; System.out.println("Connecting to " + url); PsqlConsole c= new PsqlConsole(url); try{ c.prompt(); }catch(SQLException e){ System.out.println("SQLException : " + e); }catch(IOException e){ System.out.println("Error in description"); } }else{ System.out.println("Usage: java PsqlConsole [url]"); System.out.println("url = www.domain.com/databasename"); } } } Here is the output I get (note, I cleaned up some things so the lines are a bit different below) Connecting to gecko/companywaterloo jdbc:postgresql://gecko/companywaterloo Exception in thread "main" java.lang.NullPointerExceptionat org.postgresql.Connection.ExecSQL(Connection.java:312)at org.postgresql.jdbc2.Statement.execute(Statement.java:273)at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)at org.postgresql.largeobject.LargeObjectManager.<init>(LargeObjectManager.java:106) at org.postgresql.Connection.getLargeObjectAPI(Connection.java:561)at org.postgresql.jdbc2.PreparedStatement.setBytes(PreparedStatement.java:297)at PsqlConsole.prompt(PsqlConsole.java:49)at PsqlConsole.main(PsqlConsole.java:62)
Am Die, 18 Jul 2000 schrieben Sie: > On Tue, 18 Jul 2000, Matt Fair wrote: > > > > I want to set an array of bytes to a prepared statement, but it throws a > > null pointer exception. > > Did you turn off autocommit? BLOB code must run in the context of a > transaction. I believe autocommit defaults to true, so you must explicitly > set it to false and call commit at the end of your transactions. > > Joachim Hi, where is the point to turn autocommit off ? (how do you turn it off ) I am new to PREPARED STATEMENT... Where can i find the documentation ? How much more efficient is it ? Is it similar to stored procedures/functions ? cs
On Tue, 18 Jul 2000, Matt Fair wrote: > > Here is the code I am using: > > public PsqlConsole(String url) { > try{ > ... > con = DriverManager.getConnection("jdbc:postgr..."); > con.setAutoCommit(false); > } > catch(ClassNotFoundException e) { > ... > } > catch(SQLException e) { > ... > } > finally { > //always close the database > try { > if(con != null) con.close(); This closes the connection! Once this method returns your connection is closed. Your subsequent SQL calls in the prompt() method use a closed connection. Joachim
On Tue, 18 Jul 2000, Christoph Schmidt wrote: > > where is the point to turn autocommit off ? You turn it off by calling a method on Connection. > I am new to PREPARED STATEMENT... > Where can i find the documentation ? See the JDBC documentation on Sun's Web site, or any recent Java book. > How much more efficient is it ? Unfortunately, with Postgresql PreparedStatement is not more efficient because Postgresql does not cache queries on the server. The Postgresql JDBC driver must expand the '?' placeholders on the client-side and send the complete query to the backend. Even without the performance advantage, PreparedStatement can be more convenient because it adds some value. For example, when a '?' is replaced by a string using setString() the driver will escape any single quotes that may be contained in the string. If you construct the query yourself it is your responsibility to get this right. Joachim
Thank you, I changed it and it doesn't throw a null pointer any more. Matt Fair Joachim Achtzehnter wrote: > On Tue, 18 Jul 2000, Matt Fair wrote: > > > > Here is the code I am using: > > > > public PsqlConsole(String url) { > > try{ > > ... > > con = DriverManager.getConnection("jdbc:postgr..."); > > con.setAutoCommit(false); > > } > > catch(ClassNotFoundException e) { > > ... > > } > > catch(SQLException e) { > > ... > > } > > finally { > > //always close the database > > try { > > if(con != null) con.close(); > > This closes the connection! Once this method returns your connection is > closed. Your subsequent SQL calls in the prompt() method use a closed > connection. > > Joachim
Have you issued: con.setAutoCommit(false); Before the first call to setBytes()? In theory, you should get a different exception thrown but it's the first thing to check with BLOBS. The second, can you turn on debugging (DriverManager.setLogStream(System.err);) to see the query failing. This is the second report today of an internal query failing, and I'm now wondering if something in the system tables have changed that's causing these errors - although I should have seen it when I ran the tests on 7.0 (although it may have occured in 7.0.1 or 7.0.2). Peter -- Peter Mount Enterprise Support Maidstone Borough Council Any views stated are my own, and not those of Maidstone Borough Council -----Original Message----- From: Matt Fair [mailto:matt@netasol.com] Sent: Tuesday, July 18, 2000 7:38 PM To: pgsql-interfaces@postgresql.org Subject: [INTERFACES] Connection.setBytes() I want to set an array of bytes to a prepared statement, but it throws a null pointer exception. Here is my code: String description = new String(".........4000 Bytes......."); PreparedStatement pstmt = con.prepareStatement("INSERTINTO mytable (str) VALUES (?)"); pstmt.setBytes(1,description.getBytes()); //Line 65, throws NullPointerException here pstmt.executeUpdate(); pstmt.close(); Exception in thread "main" java.lang.NullPointerExceptionat org.postgresql.Connection.ExecSQL(Connection.java:312)at org.postgresql.jdbc2.Statement.execute(Statement.java:273)at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)at org.postgresql.largeobject.LargeObjectManager.<init>(LargeObjectManager.java :106) at org.postgresql.Connection.getLargeObjectAPI(Connection.java:561)at org.postgresql.jdbc2.PreparedStatement.setBytes(PreparedStatement.java:297) ==== >at PsqlConsole.prompt(PsqlConsole.java:65) <====at PsqlConsole.main(PsqlConsole.java:87) It throws it when I call setBytes(), I don't know what the problem is. I got the source of setBytes and put it in my code where I have setBytes(). It was throwing a null pointer when it was calling getLargeObjectAPI(). Has anyone else had this error? Thanks Matt Fair
The best point is just after you get a Connection object. ie: conn.setAutoCommit(false); Then when you issue any updates, you need to call conn.commit(); to commit the changes to the database. Peter -- Peter Mount Enterprise Support Maidstone Borough Council Any views stated are my own, and not those of Maidstone Borough Council -----Original Message----- From: ChristophSchmidt [mailto:cs.hilzingen@swol.de] Sent: Tuesday, July 18, 2000 8:33 PM To: Joachim Achtzehnter; pgsql-interfaces@postgresql.org Subject: Re: [INTERFACES] Connection.setBytes() Am Die, 18 Jul 2000 schrieben Sie: > On Tue, 18 Jul 2000, Matt Fair wrote: > > > > I want to set an array of bytes to a prepared statement, but it throws a > > null pointer exception. > > Did you turn off autocommit? BLOB code must run in the context of a > transaction. I believe autocommit defaults to true, so you must explicitly > set it to false and call commit at the end of your transactions. > > Joachim Hi, where is the point to turn autocommit off ? (how do you turn it off ) I am new to PREPARED STATEMENT... Where can i find the documentation ? How much more efficient is it ? Is it similar to stored procedures/functions ? cs
Yes, now I look more closely yes, the finally clause does close the connection, which is why you get the NullPointerException. Peter -- Peter Mount Enterprise Support Maidstone Borough Council Any views stated are my own, and not those of Maidstone Borough Council -----Original Message----- From: Joachim Achtzehnter [mailto:joachim@kraut.bc.ca] Sent: Tuesday, July 18, 2000 8:48 PM To: pgsql-interfaces@postgresql.org Subject: Re: [INTERFACES] Connection.setBytes() On Tue, 18 Jul 2000, Matt Fair wrote: > > Here is the code I am using: > > public PsqlConsole(String url) { > try{ > ... > con = DriverManager.getConnection("jdbc:postgr..."); > con.setAutoCommit(false); > } > catch(ClassNotFoundException e) { > ... > } > catch(SQLException e) { > ... > } > finally { > //always close the database > try { > if(con != null) con.close(); This closes the connection! Once this method returns your connection is closed. Your subsequent SQL calls in the prompt() method use a closed connection. Joachim