0

I am unable to store image into MySQL database through the jdbc code below:

package com.jlcindia.jdbcfiles;

import java.sql.*;
import java.io.*;

public class Test1 {
public static void main(String []args){
    Connection con=null;
    PreparedStatement ps=null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jlcstudents","root","garima");
        ps=con.prepareStatement("insert into filetest (cno,file) values(1,?)");

        File image=new File("C:\\html images\\MickeyMouse.jpg");

        FileInputStream fis=new FileInputStream(image);
        ps.setBinaryStream(2, fis);
        ps.execute();
        System.out.println("Record Inserted");

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally{
        try {
            if(ps!=null)
                ps.close();
            if(con!=null)
                con.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
  }
}

Error in console is as:

Exception in thread "main" java.lang.AbstractMethodError: 
  com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;)V
  at com.jlcindia.jdbcfiles.Test1.main(Test1.java:19)
4
  • 1
    possible duplicate of Why do I get java.lang.AbstractMethodError when trying to load a blob in the db? Commented Jul 19, 2014 at 7:23
  • are you using BLOB? it helps you store any kind of files in database.. Commented Jul 19, 2014 at 7:23
  • @Inceptor361 am using LONGBLOB in MySQL Commented Jul 19, 2014 at 7:30
  • You have one parameter, and you set it by ps.setBinaryStream(2, fis);. Not sure, but shouldn't you be setting the first parameter rather than the second, which doesn't exist AFAICT. Commented Jul 19, 2014 at 9:02

1 Answer 1

1

That error so far as I am aware means that the specific method hasn't been implemented, you can try however converting the file into a byte array and storing it using ps.setBytes(<Byte Array>) instead if the column is set to storing BLOB data

Sign up to request clarification or add additional context in comments.

1 Comment

I've had a similar issue with streams on another DB provider, then tried using setBytes which worked. Definitely try this one if you can't get streams to work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.