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)
Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49
jcool
  • 314
  • 1
  • 3
  • 11
  • 1
    possible duplicate of [Why do I get java.lang.AbstractMethodError when trying to load a blob in the db?](http://stackoverflow.com/questions/1194990/why-do-i-get-java-lang-abstractmethoderror-when-trying-to-load-a-blob-in-the-db) – Jens Jul 19 '14 at 07:23
  • are you using `BLOB`? it helps you store any kind of files in database.. – Incpetor Jul 19 '14 at 07:23
  • @Inceptor361 am using LONGBLOB in MySQL – jcool Jul 19 '14 at 07: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. – TT. Jul 19 '14 at 09:02

1 Answers1

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

Dark Eye
  • 67
  • 2
  • 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. – TT. Jul 19 '14 at 09:05