Skip site navigation (1) Skip section navigation (2)

BLOBs and JDBC in PostgreSQL-6.5

From: jews(at)imada(dot)sdu(dot)dk
To: pgsql-interfaces(at)postgresql(dot)org
Subject: BLOBs and JDBC in PostgreSQL-6.5
Date: 1999-06-21 15:16:36
Message-ID: 199906211516.RAA04732@medea.imada.sdu.dk (view raw or flat)
Thread:
Lists: pgsql-interfaces
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(dot)Driver(at)da6f42d3]
Connecting to Database URL = ddl
DriverManager.getConnection("jdbc:postgresql:ddl")
    trying driver[className=postgresql.Driver,postgresql(dot)Driver(at)da6f42d3]
-- listing properties --
password=secret
user=ddl
PGDBNAME=ddl
Protocol=postgresql
Using postgresql.jdbc2.Connection
getConnection returning driver[className=postgresql.Driver,postgresql(dot)Driver(at)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.
 

pgsql-interfaces by date

Next:From: Donald JacksonDate: 1999-06-21 16:31:19
Subject: problems working with refint functions
Previous:From: Peter MountDate: 1999-06-21 10:12:37
Subject: RE: [INTERFACES] PG6.5 + JDBC + Linux + Kaffe

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group