2

I'm trying to insert an image into a database, but getting the following:

java.sql.SQLDataException: An attempt was made to get a data value of type 'BLOB' from a data value of type 'java.io.InputStream(ASCII)'.

I'm using blob in the database.

Here is how I'm doing the insertion:

package javaapplication16;
import com.sun.rowset.CachedRowSetImpl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.CachedRowSet;

public class JavaApplication16 {

    public static void main(String[] args) throws FileNotFoundException {
        try {
            String driver = "org.apache.derby.jdbc.EmbeddedDriver";
            try {
                try {
                    Class.forName(driver).newInstance();
                } catch (InstantiationException ex) {
                    Logger.getLogger(JavaApplication16.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(JavaApplication16.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(JavaApplication16.class.getName()).log(Level.SEVERE, null, ex);
            }

            CachedRowSet crs = null;
            crs = new CachedRowSetImpl();
            crs.setUrl("jdbc:derby:derbyDB; create = true");
            crs.setUsername("x");
            crs.setPassword("x");
            crs.setCommand("drop table tbl");
            crs.execute();
            crs.setCommand("CREATE TABLE tbl (ID blob)");
            crs.execute();
            File f = new File("/images/exam_gif_to_png.gif");
            crs.setCommand("insert into tbl (id) values (?)");
            FileInputStream fin = new FileInputStream(f);
            crs.setBinaryStream(1, fin, (int) f.length());
            crs.execute();
        } catch (SQLException ex) {
            Logger.getLogger(JavaApplication16.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

By the way, about storing the path of the image only, the problem is that I want users to be able to send me images and store them in a file, and I'm not sure how exactly to resolve the issue of having multiple images with the same name, will renaming be a good and simple solution?

Abdul Rahim Haddad
  • 689
  • 1
  • 8
  • 14

1 Answers1

0

Try this:

File f = new File(imagePath);
FileInputStream fin = new FileInputStream(f);
crs.setBinaryStream(5, fin, (int) f.length());

EDIT --- EDIT

Try this:

I think your problem is that you are not propagating changes.

Call acceptChanges() after every command.

crs.setCommand("drop table tbl");
crs.execute();
crs.acceptChanges();

crs.setCommand("CREATE TABLE tbl (ID blob)");
crs.execute();
crs.acceptChanges();

crs.setBinaryStream(1, fin, (int) f.length());
crs.execute();
crs.acceptChanges();
jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • Added you mentioned. Got this exception for the line crs.acceptChanges(); done after creating the table: Exception in thread "main" java.lang.NullPointerException at com.sun.rowset.internal.CachedRowSetWriter.initSQLStatements(CachedRowSetWriter.java:1064) at com.sun.rowset.internal.CachedRowSetWriter.writeData(CachedRowSetWriter.java:300) at com.sun.rowset.CachedRowSetImpl.acceptChanges(CachedRowSetImpl.java:880) at javaapplication16.JavaApplication16.main(JavaApplication16.java:41) Java Result: 1 – Abdul Rahim Haddad Apr 24 '13 at 13:58