import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.io.IOException;
import java.sql.SQLException;


public class testLob{

    public static void main(String[] args) throws SQLException,ClassNotFoundException,IOException  {
	
	Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

	Connection conn = DriverManager.getConnection("jdbc:derby:testdb;create=true");
	Statement st = conn.createStatement();
	try{
	    st.execute("drop table a");
	}catch(SQLException e){
	    e.printStackTrace();
	}

	st.execute("create table a(b blob)");
	st.close();
	
	PreparedStatement pst = conn.prepareStatement("insert into a(b) values(?)");

	byte[] data = new byte[1024 * 1024];
	for(int i = 0;
	    i < 1024 * 1024;
	    i  ++){
	    data[i] = (byte)(i % 256);
	}

	pst.setBinaryStream(1,new ByteArrayInputStream(data),data.length);
	pst.executeUpdate();
	pst.close();
	
	
	st = conn.createStatement();
	ResultSet rs = st.executeQuery("select b from a");
	rs.next();
	
	InputStream is = rs.getBinaryStream(1);
	System.out.println("Here goes first stream");
	System.out.println(is.read());
	System.out.println(is.read());
	System.out.println(is.read());
	
	is = rs.getBinaryStream(1);
	System.out.println("Here goes 2nd stream");
	System.out.println(is.read());
	System.out.println(is.read());
	System.out.println(is.read());
	
	rs.close();
	st.close();

	conn.close();

    }

}
