1

I'm trying to persist an InputStream representing an image in Oracle database as below:

    Connection conn = null; 
    String message = null;  

    try {
        // connects to the database
        Class.forName("oracle.jdbc.driver.OracleDriver");  

           conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","great123");

        String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, firstName);
        statement.setString(2, lastName);

        if (inputStream != null) {
             column
            statement.setBlob(3, inputStream);
        }


        int row = statement.executeUpdate();
        if (row > 0) {
            message = "File uploaded and saved into database";
        }
    } finally {
        if (conn != null) {
            // closes the database connection
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

However, it throws the below exception:

console output: Servlet.service() for servlet [com.sandy.FileUploadDBServlet] in context with path [/TestAgent] threw exception [Servlet execution threw an exception] with root cause java.lang.AbstractMethodError: oracle.jdbc.driver.OraclePreparedStatementWrapper.setBinaryStream(ILjava/io/InputStream;)

I have tried statement.setBlob(3, inputStream); as well as statement.setBinary(3, inputStream);, but no go.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sandeep Deb
  • 51
  • 1
  • 8
  • You seem to be using an old Oracle JDBC driver that does not implement the method PreparedStatement.setBinaryStream(), introduced in Java 6. Upgrade your JDBC driver. – JB Nizet Jul 19 '15 at 09:58
  • I'm using odbc14.jar. Doesn't it support PreparedStatement.setBinaryStream()? – Sandeep Deb Jul 19 '15 at 11:07
  • No. ojdbc14 is for JDK 1.4. setBinaryStream() has been introduced in Java 6. – JB Nizet Jul 19 '15 at 13:00
  • I have tried ojdbc7( For use with JDK 7; It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.) I have pasted the ojdbc7.jar file in jre/lib/ext folder. Also tired with D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc7.jar and D:\tomcat7.0\lib. – Sandeep Deb Jul 19 '15 at 15:32
  • I have tried ojdbc7( For use with JDK 7; It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.) I have pasted the ojdbc7.jar file in jre/lib/ext folder. Also tired with D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc7.jar and D:\tomcat7.0\lib. Set the class path D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc7.jar. Where am I going wrong? – Sandeep Deb Jul 19 '15 at 15:38

1 Answers1

0

When you use jdbc, this kind of error can occur when jdbc driver that you use implements older version of jdbc api, than that one which is included into your JRE. You could check these versions.

I`m not expert with jdbc but i think i read about this problem earlier.

Good luck.

Adrian Deja
  • 737
  • 1
  • 9
  • 17
  • I have tried ojdbc7( For use with JDK 7; It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types.) I have pasted the ojdbc7.jar file in jre/lib/ext folder. Also tired with D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc7.jar and D:\tomcat7.0\lib. Set the class path D:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc7.jar. Where am I going wrong? – Sandeep Deb Jul 19 '15 at 15:40