Обсуждение: JDBC tutorial code
Hello, I have followed the JDBC tutorial code at http://www.postgresql.org/devel-corner/docs/programmer/jdbc-lo.htm and keep getting an sqlException from the JDBC driver. I need to deal with BLOBS. Text fields etc work fine. The exception states: InputStream not supported. The code is as follows: import java.sql.*; import java.io.*; public class dbtest { public static void main( String[] args ) { // Load the driver try { // Load the driver class Class.forName( "org.postgresql.Driver" ); // Define the data source for the driver String dbURL = "jdbc:postgresql://192.168.0.3/mynewdb"; // Create a connection through the DriverManager Connection dbCon = DriverManager.getConnection( dbURL, USER , PASS ); dbCon.setAutoCommit( false ); File file = new File( "D:\\dev\\code\\dbtest\\jdbc7.0-1.2.jar" ); FileInputStream fis = new FileInputStream( file ); PreparedStatement ps = dbCon.prepareStatement( "insert into test1 values (?,?)" ); ps.setString( 1, file.getName() ); ps.setBinaryStream( 2, fis, ( int )file.length() ); ps.executeUpdate(); ps.close(); fis.close(); } catch( ClassNotFoundException cnfe ) { System.err.println( "1 " + cnfe ); } catch( SQLException sqle ) { System.err.println( "2 " + sqle ); sqle.printStackTrace(System.err); } catch( FileNotFoundException fnfe ) { System.err.println( "3 " + fnfe ); } catch( IOException ioe ) { System.err.println( "4 " + ioe ); } } Thanks in advance for any help. Regards, Neil ---------------------------------------------------------------- From: Neil Kidd Website: http://www.kidd.org.uk E-mail: neil@kidd.org.uk ----------------------------------------------------------------
Neil,
For some reason, postgres' JDBC driver doesn't implement
PreparedStatement.setBinaryStream().
Use PreparedStatement.setBytes() instead.
Try this...
InputStream is = new BufferedInputStream(
new FileInputStream( file ) );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int b = is.read(); b >=0; b = is.read())
baos.write(b);
ps.setBytes(2, baos.toByteArray());
Hope this helps,
Steve
On Tue, Dec 12, 2000 at 11:55:06AM +0000, Neil Kidd wrote:
>
> Hello,
> I have followed the JDBC tutorial code at
> http://www.postgresql.org/devel-corner/docs/programmer/jdbc-lo.htm
> and keep getting an sqlException from the JDBC driver. I need to deal with
> BLOBS. Text fields etc work fine.
> The exception states: InputStream not supported.
>
> The code is as follows:
>
> import java.sql.*;
> import java.io.*;
>
> public class dbtest
> {
> public static void main( String[] args )
> {
> // Load the driver
> try
> {
> // Load the driver class
> Class.forName( "org.postgresql.Driver" );
>
> // Define the data source for the driver
> String dbURL = "jdbc:postgresql://192.168.0.3/mynewdb";
>
> // Create a connection through the DriverManager
> Connection dbCon = DriverManager.getConnection( dbURL, USER , PASS );
>
> dbCon.setAutoCommit( false );
>
> File file = new File(
> "D:\\dev\\code\\dbtest\\jdbc7.0-1.2.jar" );
> FileInputStream fis = new FileInputStream( file );
> PreparedStatement ps = dbCon.prepareStatement( "insert into test1
> values (?,?)" );
>
> ps.setString( 1, file.getName() );
> ps.setBinaryStream( 2, fis, ( int )file.length() );
> ps.executeUpdate();
> ps.close();
> fis.close();
> }
> catch( ClassNotFoundException cnfe )
> {
> System.err.println( "1 " + cnfe );
> }
> catch( SQLException sqle )
> {
> System.err.println( "2 " + sqle );
> sqle.printStackTrace(System.err);
> }
> catch( FileNotFoundException fnfe )
> {
> System.err.println( "3 " + fnfe );
> }
> catch( IOException ioe )
> {
> System.err.println( "4 " + ioe );
> }
> }
>
> Thanks in advance for any help.
> Regards,
> Neil
> ----------------------------------------------------------------
> From: Neil Kidd
> Website: http://www.kidd.org.uk
> E-mail: neil@kidd.org.uk
> ----------------------------------------------------------------
Steve, Thanks very much, it works a treat :-))) Merry xmas, Neil. At 13:00 12/12/00 -0500, you wrote: >Neil, > >For some reason, postgres' JDBC driver doesn't implement >PreparedStatement.setBinaryStream(). >Use PreparedStatement.setBytes() instead. > >Try this... > > InputStream is = new BufferedInputStream( > new FileInputStream( file ) ); > ByteArrayOutputStream baos = new ByteArrayOutputStream(); > for (int b = is.read(); b >=0; b = is.read()) > baos.write(b); > ps.setBytes(2, baos.toByteArray()); > >Hope this helps, > Steve > > >On Tue, Dec 12, 2000 at 11:55:06AM +0000, Neil Kidd wrote: > > > > Hello, > > I have followed the JDBC tutorial code at > > http://www.postgresql.org/devel-corner/docs/programmer/jdbc-lo.htm > > and keep getting an sqlException from the JDBC driver. I need to deal with > > BLOBS. Text fields etc work fine. > > The exception states: InputStream not supported. > > > > The code is as follows: > > > > import java.sql.*; > > import java.io.*; > > > > public class dbtest > > { > > public static void main( String[] args ) > > { > > // Load the driver > > try > > { > > // Load the driver class > > Class.forName( "org.postgresql.Driver" ); > > > > // Define the data source for the driver > > String dbURL = "jdbc:postgresql://192.168.0.3/mynewdb"; > > > > // Create a connection through the DriverManager > > Connection dbCon = DriverManager.getConnection( dbURL, USER , > PASS ); > > > > dbCon.setAutoCommit( false ); > > > > File file = new File( > > "D:\\dev\\code\\dbtest\\jdbc7.0-1.2.jar" ); > > FileInputStream fis = new FileInputStream( file ); > > PreparedStatement ps = dbCon.prepareStatement( "insert into test1 > > values (?,?)" ); > > > > ps.setString( 1, file.getName() ); > > ps.setBinaryStream( 2, fis, ( int )file.length() ); > > ps.executeUpdate(); > > ps.close(); > > fis.close(); > > } > > catch( ClassNotFoundException cnfe ) > > { > > System.err.println( "1 " + cnfe ); > > } > > catch( SQLException sqle ) > > { > > System.err.println( "2 " + sqle ); > > sqle.printStackTrace(System.err); > > } > > catch( FileNotFoundException fnfe ) > > { > > System.err.println( "3 " + fnfe ); > > } > > catch( IOException ioe ) > > { > > System.err.println( "4 " + ioe ); > > } > > } > > > > Thanks in advance for any help. > > Regards, > > Neil > > ---------------------------------------------------------------- > > From: Neil Kidd > > Website: http://www.kidd.org.uk > > E-mail: neil@kidd.org.uk > > ---------------------------------------------------------------- ---------------------------------------------------------------- From: Neil Kidd Website: http://www.kidd.org.uk E-mail: neil@kidd.org.uk ----------------------------------------------------------------