pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jews@imada.sdu.dk
To: pgsql-interfaces@postgresql.org
Subject: BLOBs and JDBC in PostgreSQL-6.5
Date: Mon, 21 Jun 1999 17:16:36 +0200 (MEST)
Message-ID: <199906211516.RAA04732@medea.imada.sdu.dk> (raw)

Hi.

I hope that someone can help me with this problem.

I can't get BLOBs to work with the JDBC driver :(

I have the following program:
//////////////////////////////////////////////////////////////////////
import java.io.*;
import java.sql.*;
import java.text.*;

public class basic
{
  Connection db;	// The connection to the database
  Statement  st;	// Our statement to run queries with
  
  public basic() throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
  {
    // Load the driver
    Class.forName("postgresql.Driver");
    
    // Connect to database
    System.out.println("Connecting to Database URL = ddl");
    db = DriverManager.getConnection("jdbc:postgresql:ddl", "ddl", "secret");
    db.setAutoCommit(false);
     
    System.out.println("Connected...");
    st = db.createStatement();
    
    example();
    
    st.executeUpdate("commit");

    // Finally close the database
    System.out.println("Now closing the connection");
    st.close();
    db.close();
  }
  
  public void example() throws SQLException
  {
      try {
	  System.out.println("Creating table ...");
	  st.executeUpdate("drop table images");
	  st.executeUpdate("create table images (imgname name, imgoid oid)");
	  
	  System.out.println("Creating file stream ...");
	  File file = new File("myimage.gif");
	  FileInputStream fis = new FileInputStream(file);

	  System.out.println("Creating statement ...");
	  PreparedStatement ps = db.prepareStatement("insert into images values (?,?)");
	  System.out.println("1");
	  ps.setString(1,file.getName());
	  System.out.println("2");
	  ps.setBinaryStream(2,fis,(int)file.length());
	  System.out.println("3");

	  System.out.println("Executing statement ...");
	  ps.executeUpdate();

	  System.out.println("Closing statement ...");
	  ps.close();
	  fis.close();
      } catch (IOException ex) {
	  System.err.println("Exception caught.\n"+ex);
	  ex.printStackTrace();
      }
  }
  
  public static void main(String args[])
  {
    System.out.println("PostgreSQL BLOB test");
    
    DriverManager.setLogStream(System.err);
    
    // Now run the tests
    try {
      basic test = new basic();
    } catch(Exception ex) {
      System.err.println("Exception caught.\n"+ex);
      ex.printStackTrace();
    }
  }
}

//////////////////////////////////////////////////////////////////////

I get this when I run it:
PostgreSQL BLOB test
DriverManager.initialize: jdbc.drivers = null
JDBC DriverManager initialized
registerDriver: driver[className=postgresql.Driver,postgresql.Driver@da6f42d3]
Connecting to Database URL = ddl
DriverManager.getConnection("jdbc:postgresql:ddl")
    trying driver[className=postgresql.Driver,postgresql.Driver@da6f42d3]
-- listing properties --
password=secret
user=ddl
PGDBNAME=ddl
Protocol=postgresql
Using postgresql.jdbc2.Connection
getConnection returning driver[className=postgresql.Driver,postgresql.Driver@da6f42d3]
Connected...
Creating table ...
Creating file stream ...
Creating statement ...
1
2
Exception caught.
java.lang.NullPointerException
java.lang.NullPointerException
        at java.io.Writer.write(Compiled Code)
        at java.io.PrintStream.write(Compiled Code)
        at java.io.PrintStream.print(Compiled Code)
        at java.io.PrintStream.println(Compiled Code)
        at java.lang.Throwable.printStackTrace(Compiled Code)
        at java.sql.SQLException.<init>(Compiled Code)
        at postgresql.util.PSQLException.<init>(Compiled Code)
        at postgresql.jdbc2.PreparedStatement.setBinaryStream(Compiled Code)
        at basic.example(Compiled Code)
        at basic.<init>(Compiled Code)
        at basic.main(Compiled Code)                                

I tried Tatsuo Ishii patch to blobtest.java and it worked ok.
But I want to use the native JDBC interface.

Any ideas ?

Cheers,

  Jesper Pedersen

P.S. I'm not on the mailing-list.
 



Message-ID: <199906211516.RAA04732@medea.imada.sdu.dk>
Permalink:  ../199906211516.RAA04732@medea.imada.sdu.dk/
Also on:    postgresql.org/message-id/199906211516.RAA04732@medea.imada.sdu.dk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jews@imada.sdu.dk
  Subject: Re: BLOBs and JDBC in PostgreSQL-6.5
  In-Reply-To: <199906211516.RAA04732@medea.imada.sdu.dk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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