BLOBs and JDBC in PostgreSQL-6.5
BLOBs and JDBC in PostgreSQL-6.5
От:
jews@imada.sdu.dk
Дата:
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.(Compiled Code) at postgresql.util.PSQLException.(Compiled Code) at postgresql.jdbc2.PreparedStatement.setBinaryStream(Compiled Code) at basic.example(Compiled Code) at basic.(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.