Saturday, 14 June 2014


Insert a image in Data Base using Java




File file = new File(imageURL);
FileInputStream input;
byte[] data = null;
try {
    input = new FileInputStream(file);
    data = new byte[input.available()];
    input.close();
} catch (Exception e) {
    e.printStackTrace();
}
Then I build my Image object: Imagen ima = new Imagen(0, data, new Date()); and I send it into another manager. I get converted the byte[] into a BLOB object like this:
    byte[] datos = image.getDatos();
    Blob blob = null;
    try {
        blob = conection.createBlob();
        blob.setBytes(1,datos);
    } catch (SQLException e) {          
        e.printStackTrace();
    }
 
    java.sql.Date sqlDate = new java.sql.Date(image.getFecha().getTime());
 
    String query = " INSERT INTO imagen VALUES('" + image.getId() + "','"
            + blob + "','" + sqlDate + "') ";
 
    int lastID = BBDD.add(query, conection);

I can run this without problems but I only get something like this "com.mysql.jdbc.Blob@1e1962d" and my MySQL doesn´t show my anything else.


No comments:

Post a Comment