简体   繁体   中英

Java - Storing image as a Blob in JavaDB

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM