pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedConnection.setBytes()
12+ messages / 4 participants
[nested] [flat]
* Connection.setBytes()
@ 2000-07-18 18:38 Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
0 siblings, 1 reply; 12+ messages in thread
From: Matt Fair @ 2000-07-18 18:38 UTC (permalink / raw)
To: pgsql-interfaces
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("INSERT INTO
mytable (str) VALUES (?)");
pstmt.setBytes(1,description.getBytes()); //Line 65, throws
NullPointerException here
pstmt.executeUpdate();
pstmt.close();
Exception in thread "main" java.lang.NullPointerException
at 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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
@ 2000-07-18 18:54 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 19:02 ` Re: Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 19:06 ` Re: [INTERFACES] Connection.setBytes() ChristophSchmidt <cs.hilzingen@swol.de>
2000-07-18 19:08 ` Re: Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 19:32 ` Re: Connection.setBytes() ChristophSchmidt <cs.hilzingen@swol.de>
0 siblings, 4 replies; 12+ messages in thread
From: Joachim Achtzehnter @ 2000-07-18 18:54 UTC (permalink / raw)
To: pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
@ 2000-07-18 19:02 ` Matt Fair <matt@netasol.com>
3 siblings, 0 replies; 12+ messages in thread
From: Matt Fair @ 2000-07-18 19:02 UTC (permalink / raw)
To: Joachim Achtzehnter <joachim@kraut.bc.ca>; +Cc: pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: [INTERFACES] Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
@ 2000-07-18 19:06 ` ChristophSchmidt <cs.hilzingen@swol.de>
3 siblings, 0 replies; 12+ messages in thread
From: ChristophSchmidt @ 2000-07-18 19:06 UTC (permalink / raw)
To: Joachim Achtzehnter <joachim@kraut.bc.ca>; pgsql-novice@postgresql.org
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
@ 2000-07-18 19:08 ` Matt Fair <matt@netasol.com>
2000-07-18 19:48 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
3 siblings, 1 reply; 12+ messages in thread
From: Matt Fair @ 2000-07-18 19:08 UTC (permalink / raw)
To: Joachim Achtzehnter <joachim@kraut.bc.ca>; +Cc: pgsql-interfaces
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("INSERT INTO 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("SQL Exception : " + 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.NullPointerException
at 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)
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 19:08 ` Re: Connection.setBytes() Matt Fair <matt@netasol.com>
@ 2000-07-18 19:48 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 20:00 ` Re: Connection.setBytes() Matt Fair <matt@netasol.com>
0 siblings, 1 reply; 12+ messages in thread
From: Joachim Achtzehnter @ 2000-07-18 19:48 UTC (permalink / raw)
To: pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 19:08 ` Re: Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 19:48 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
@ 2000-07-18 20:00 ` Matt Fair <matt@netasol.com>
0 siblings, 0 replies; 12+ messages in thread
From: Matt Fair @ 2000-07-18 20:00 UTC (permalink / raw)
To: Joachim Achtzehnter <joachim@kraut.bc.ca>; +Cc: pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
@ 2000-07-18 19:32 ` ChristophSchmidt <cs.hilzingen@swol.de>
2000-07-18 19:57 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
3 siblings, 1 reply; 12+ messages in thread
From: ChristophSchmidt @ 2000-07-18 19:32 UTC (permalink / raw)
To: Joachim Achtzehnter <joachim@kraut.bc.ca>; pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* Re: Connection.setBytes()
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Re: Connection.setBytes() Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 19:32 ` Re: Connection.setBytes() ChristophSchmidt <cs.hilzingen@swol.de>
@ 2000-07-18 19:57 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
0 siblings, 0 replies; 12+ messages in thread
From: Joachim Achtzehnter @ 2000-07-18 19:57 UTC (permalink / raw)
To: pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* RE: Connection.setBytes()
@ 2000-07-19 06:47 Peter Mount <petermount@it.maidstone.gov.uk>
0 siblings, 0 replies; 12+ messages in thread
From: Peter Mount @ 2000-07-19 06:47 UTC (permalink / raw)
To: 'Matt Fair' <matt@netasol.com>; pgsql-interfaces
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("INSERT INTO
mytable (str) VALUES (?)");
pstmt.setBytes(1,description.getBytes()); //Line 65, throws
NullPointerException here
pstmt.executeUpdate();
pstmt.close();
Exception in thread "main" java.lang.NullPointerException
at 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
^ permalink raw reply [nested|flat] 12+ messages in thread
* RE: Connection.setBytes()
@ 2000-07-19 06:54 Peter Mount <petermount@it.maidstone.gov.uk>
0 siblings, 0 replies; 12+ messages in thread
From: Peter Mount @ 2000-07-19 06:54 UTC (permalink / raw)
To: 'cs.hilzingen@swol.de' <cs.hilzingen@swol.de>; Joachim Achtzehnter <joachim@kraut.bc.ca>; pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
* RE: Connection.setBytes()
@ 2000-07-19 06:56 Peter Mount <petermount@it.maidstone.gov.uk>
0 siblings, 0 replies; 12+ messages in thread
From: Peter Mount @ 2000-07-19 06:56 UTC (permalink / raw)
To: 'Joachim Achtzehnter' <joachim@kraut.bc.ca>; pgsql-interfaces
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
^ permalink raw reply [nested|flat] 12+ messages in thread
end of thread, other threads:[~2000-07-19 06:56 UTC | newest]
Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-07-18 18:38 Connection.setBytes() Matt Fair <matt@netasol.com>
2000-07-18 18:54 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 19:02 ` Matt Fair <matt@netasol.com>
2000-07-18 19:06 ` ChristophSchmidt <cs.hilzingen@swol.de>
2000-07-18 19:08 ` Matt Fair <matt@netasol.com>
2000-07-18 19:48 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-18 20:00 ` Matt Fair <matt@netasol.com>
2000-07-18 19:32 ` ChristophSchmidt <cs.hilzingen@swol.de>
2000-07-18 19:57 ` Joachim Achtzehnter <joachim@kraut.bc.ca>
2000-07-19 06:47 RE: Connection.setBytes() Peter Mount <petermount@it.maidstone.gov.uk>
2000-07-19 06:54 RE: Connection.setBytes() Peter Mount <petermount@it.maidstone.gov.uk>
2000-07-19 06:56 RE: Connection.setBytes() Peter Mount <petermount@it.maidstone.gov.uk>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox