1 Reply Latest reply: Feb 12, 2013 12:49 PM by Mihael RSS

    NoClassDefFoundError error trying to insert blob to a oracle 11g table

    Raj_OGG-Oracle
      Here is the java code. I have the real values in place of @<host name>:<port>:<sid>

      import oracle.sql.*;
      import java.sql.*;
      import java.io.*;
      import java.util.*;
      import oracle.jdbc.*;
      //import java.text.DateFormat;
      //import java.util.Date;


      class blobinsert
      {
      public static void main (String args [])
      {
      try {
      // Load the Oracle JDBC driver
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
      Connection connOracle = DriverManager.getConnection("jdbc:oracle:thin:@<host name>:<port>:<sid>", "hr", "hr");

      File binaryFile = new File("c:/test.gif");
      System.out.println("test pdf length = " + binaryFile.length());
      FileInputStream instream = new FileInputStream(binaryFile);

      PreparedStatement ps = connOracle.prepareStatement(
      "insert into test (id, blob_file) values(?, empty_blob())");
      ps.setInt(1, 100);
      ps.executeUpdate();
      ps.close();

      ps = connOracle.prepareStatement(
      "select blob_file from test where id = ? for update");
      ps.setInt(1, 100);
      ResultSet rs = ps.executeQuery();
      if (rs.next()) {
      BLOB blob = ((OracleResultSet) rs).getBLOB("blob_file");

      OutputStream outputStream = blob.getBinaryOutputStream();

      byte[] buffer = new byte[blob.getBufferSize()];
      int position = 1;
      int bytesRead =0;
      int bytesWritten =0;
      int totbytesRead =0;
      int totbytesWritten =0;
      while ((bytesRead = instream.read(buffer)) != -1) {
      bytesWritten = blob.putBytes(position, buffer, bytesRead);
      position += bytesRead;
      totbytesRead += bytesRead;
      totbytesWritten += bytesWritten;
      System.out.println("processing "+bytesRead+" bytes");
      }
      outputStream.close();
      instream.close();
      connOracle.close();
      }
      } catch (Exception e) {     e.printStackTrace();   }

      }
      }

      I compiled it using following statement

      cmd> javac.exe -Xlint:deprecation insert.java

      insert.java:37: warning: [deprecation] getBinaryOutputStream() in oracle.sql.BLO
      B has been deprecated
      OutputStream outputStream = blob.getBinaryOutputStream();
      ^
      insert.java:46: warning: [deprecation] putBytes(long,byte[],int) in oracle.sql.B
      LOB has been deprecated
      bytesWritten = blob.putBytes(position, buffer, bytesRead);
      ^
      2 warnings

      Executed it as below

      cmd> java blobinsert.class

      I get an error as below

      Exception in thread "main" java.lang.NoClassDefFoundError: blobinsert/class
      Caused by: java.lang.ClassNotFoundException: blobinsert.class
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      Could not find the main class: blobinsert.class. Program will exit.

      Any ideas what is wrong ?

      The table can be created as below

      sqlplus hr

      CREATE TABLE TEST
      (
      ID NUMBER(9),
      BLOB_FILE BLOB
      )
      LOB (BLOB_FILE) STORE AS (
      TABLESPACE USERS
      ENABLE STORAGE IN ROW
      CHUNK 8192
      RETENTION
      NOCACHE
      LOGGING
      INDEX (TABLESPACE USERS))
      TABLESPACE USERS
      LOGGING
      NOCOMPRESS
      NOCACHE
      NOPARALLEL
      MONITORING;

      C:\Users\rgovindaswamy\Desktop\3-6486556511-blob-java>echo %PATH%

      C:\APP\rgovindaswamy\product\11.2.0\dbhome_1\Opatch;C:\APP\rgovindaswamy\product
      \11.2.0\dbhome_1\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live
      ;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\windows\sy
      stem32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell
      \v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Blu
      etooth Software\syswow64;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program
      Files (x86)\Windows Live\Shared;C:\Program Files (x86)\grep-2.5.4-bin\;C:\Window
      s\System32;C:\Program Files\Java\jdk1.6.0_27\jre\bin;C:\Program Files\Java\jdk1.
      6.0_27\jre\bin\server;C:\windows\Microsoft.NET\Framework\v2.0.50727;C:\Program F
      iles (x86)\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.6.0_27\bin;C:\app\rgov
      indaswamy\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar;C:\Program Files (x86)\IDM
      Computer Solutions\UltraEdit\;C:\Users\rgovindaswamy\Documents\UltraEdit\;C:\Pr
      ogram Files (x86)\grep-2.5.4-bin\

      C:\Users\rgovindaswamy\Desktop\3-6486556511-blob-java>echo %CLASSPATH%

      .;C:\app\rgovindaswamy\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar;C:\Program Fi
      les (x86)\Java\jre6\lib\ext\QTJava.zip\;C:\Program Files\Java\jdk1.6.0_27\bin

      The jar file in use is C:\app\rgovindaswamy\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar

      My db version is 11.2.0.3.0 on windows 7 x64

      C:\Users\rgovindaswamy\Desktop\3-6486556511-blob-java>java -version
      java version "1.6.0_27"
      Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)