0

Here is my code

    Connection con = null;
    PreparedStatement ps = null;
    InputStream is = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.
                getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
                    ,"user","password");
        ps = con.prepareCall("insert into student_profile values (?,?)");
        ps.setInt(1, 101);
        is = new FileInputStream(new File("Student_img.jpg"));
        ps.setBinaryStream(2, is);
        int count = ps.executeUpdate();
        System.out.println("Count: "+count);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{
        try{
            if(is != null) is.close();
            if(ps != null) ps.close();
            if(con != null) con.close();
        } catch(Exception ex){}
    }
}

}

I Don't Know where I am wrong but there is an error in executing the code Exception I get

  • Exception in thread "main" java.lang.AbstractMethodError: Method oracle/jdbc/driver/OraclePreparedStatementWrapper.setBlob(ILjava/io/InputStream;)V is abstract at oracle.jdbc.driver.OraclePreparedStatementWrapper.setBlob(OraclePreparedStatementWrapper.java)
Vivek Anoop
  • 7
  • 1
  • 6
  • 2
    Might be unrelated by see http://stackoverflow.com/questions/1194990/why-do-i-get-java-lang-abstractmethoderror-when-trying-to-load-a-blob-in-the-db – Moob Mar 13 '15 at 11:47
  • Your error message doesn't fit your code. You are not calling `setBlob()` in your code. The code you have shown should work just fine apart from the fact that you should use `prepareStatement()` not `prepareCall()` - which is for calling stored procedures. (Btw. `oracle.jdbc.driver.OracleDriver` is deprecated, use `oracle.jdbc.OracleDriver` instead). –  Mar 13 '15 at 11:58
  • @a_horse_with_no_name Its still displaying the same error message even with the changes you suggested – Vivek Anoop Mar 13 '15 at 12:01

3 Answers3

0

Please do not downvote, I don't have 50 reputation to add a comment. Just point me the problem with my answer and I'll remove it eventually.

I don't know if it's the right answer, but in my case I had to switch to oracle driver version 6. Previous drivers had problems with Blobs. In maven:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>oracle-jdbc</artifactId>
    <version>6</version>
</dependency>
musikele
  • 355
  • 2
  • 15
0

Use get getBlob() method to get image from database using ResultSet.

ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
      pstmt = conn.prepareStatement(query);
      pstmt.setInt(1, id);
      rs = pstmt.executeQuery();
      rs.next();
      Blob blob = rs.getBlob("photo");
      // materialize BLOB onto client
      return blob.getBytes(1, (int) blob.length());
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }

Code Source

Insert BLOG(Picture or Photo) Data Type Into Oracle Database

Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

use setBinaryStream() method

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertPictureToMySql {
public static void main(String[] args) throws Exception,    IOException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)";

FileInputStream fis = null;
PreparedStatement ps = null;
try {
  conn.setAutoCommit(false);
  File file = new File("myPhoto.png");
  fis = new FileInputStream(file);
  ps = conn.prepareStatement(INSERT_PICTURE);
  ps.setString(1, "001");
  ps.setString(2, "name");
  ps.setBinaryStream(3, fis, (int) file.length());
  ps.executeUpdate();
  conn.commit();
} finally {
  ps.close();
  fis.close();
}
}
}
subhakar patnala
  • 319
  • 1
  • 4
  • 15