Blobs with JDBC 2.0

From: Scott Serr <scott.serr_at_usa.net>
Date: Sat, 21 Jul 2001 21:49:21 GMT
Message-ID: <2a540333.0106070655.26fa481f_at_posting.google.com>


I have a test program written in Java 2 (1.3.0) shows some oddness in streaming more than 2000 chars into a blob. I rather not use the Oracle specific calls, in theory JDBC 2.0 includes all the blob stuff so we shouldn't have to use Oracle specific calls. I'm open to any advise.

Here it is: (name it t2.java if you would like to test it)


import java.util.*;
import java.io.*;
import java.sql.*;

public class t2 {

    public static void main(String[] args) {        
        try {
	    if (args.length != 6) {
		System.err.println("Usage: java -classpath thinoracledriver.zip t2
host sid user password sourcefile destfile");
		return;
	    }

	    System.err.println();
	    System.err.println("  Connecting To The Database");
            DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver());
	    Connection c = DriverManager.getConnection( "jdbc:oracle:thin:_at_"
+ args[0] +":1521:" + args[1] , args[2], args[3]);
            System.err.println("  Creating Table");
            Statement s = c.createStatement();
	    try {
		s.executeUpdate("DROP TABLE dookie");
	    } catch (Exception ee) {}
            s.executeUpdate("CREATE TABLE dookie (id number, big
blob)");
            s.executeUpdate("INSERT INTO dookie (id) values (1)");
	    
	    System.err.println("  Opening Source File");
            File f = new File(args[4]);
	    System.err.println("  Source File Length = " + (int)f.length());
            FileInputStream sb = new FileInputStream(f);

	    System.err.println("  Preparing Update Statement");
            PreparedStatement pstmt = c.prepareStatement("UPDATE
dookie SET big = ? WHERE id=1");
            pstmt.setBinaryStream(1, sb , (int)f.length());
            pstmt.executeUpdate();
	    System.err.println("  Update Complete");
	    
	    
	    System.err.println();
	    System.err.println("  Querying Table");
            ResultSet rs = s.executeQuery("SELECT big FROM dookie
WHERE id=1");
	    System.err.println("  .next");
            rs.next();
	    System.err.println("  .getBinaryStream");
            InputStream foo = rs.getBinaryStream("big");
            FileOutputStream filestream = new
FileOutputStream(args[5]);
	    System.err.println("  Looping Through The Streams");
            int i = -1;
	    while((i = foo.read())!=-1) {
		filestream.write(i);
            }
            filestream.close();

	    System.err.println("  Dropping The Table");
            s.executeUpdate("DROP TABLE dookie");
	    s.close();
	    c.close();
	    
    	} catch (Exception e) { 
	    System.err.println("TEST PROGRAM EXCEPTION: "+e);
	}
	
	

    System.err.println();
    }
} Received on Sat Jul 21 2001 - 23:49:21 CEST

Original text of this message