Uploading an image to a database with javafx

杀马特。学长 韩版系。学妹 提交于 2021-01-29 21:46:22

问题


i'm trying to upload an image to mysql database using phpmyadmin and despite the program works and stores the rest of the data the image is not stored correctly.

If I upload an image directly in phpmyadmin the image's size in type Blob it's 26.6 KB but if I try to uploading using javafx the image's size it's about 10 B so I think i'm not uploading correctly.

@Override
public void guardarMonstruo(MonstruoDTO monstruo) {
    con= ConexionBDA.getInstance().getCon();
    try {
    if (con != null){
        byte[] blob = imagenToByte(monstruo.getImagen());
        System.out.println(blob.toString());
        statement = con.createStatement();
        statement.executeUpdate("INSERT INTO monstruos (Nombre,Habitat,Estado,ColaCercenable,DragonAnciano,DebilidadFuego,DebilidadAgua,Debilidadrayo,DebilidadHielo,DebilidadDraco,ImagenMonstruo) VALUES ('"+monstruo.getNombre()+"'"+","+"'"+monstruo.getHabitat()+"'"+","+"'"+monstruo.getEstado()+"'"+","+"'"+monstruo.getColaCercenable()+"'"+","+"'"+monstruo.getDragonAnciano()+"'"+","+"'"+monstruo.getDebilidadFuego()+"'"+","+"'"+monstruo.getDebilidadAgua()+"'"+","+"'"+monstruo.getDebilidadRayo()+"'"+","+"'"+monstruo.getDebilidadHielo()+"'"+","+"'"+monstruo.getDebilidadDraco()+"'"+","+"'"+blob+"');");
        con.close();
        statement.close();


    }
    }
    catch(Exception e) {
        e.printStackTrace();
    }


}

And the method imagenToByte() is used to pass the image to byte is this:

private byte[] imagenToByte(Image imagen) {
    BufferedImage bufferimage = SwingFXUtils.fromFXImage(imagen, null);
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      try {
        ImageIO.write(bufferimage, "jpg", output );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      byte [] data = output.toByteArray();
      return data;
}

I don't know what i'm doing wrong, could you please help me?


回答1:


You insert the result of the toString method to the query string which won't result in the desired outcome. Use PreparedStatement and set a Blob parameter instead:

try (PreparedStatement ps = con.prepareStatement("INSERT INTO monstruos (Nombre,Habitat,Estado,ColaCercenable,DragonAnciano,DebilidadFuego,DebilidadAgua,Debilidadrayo,DebilidadHielo,DebilidadDraco,ImagenMonstruo) VALUES (?,?,?,?,?,?,?,?,?,?,?)")) {
    ps.setString(1, monstruo.getNombre());
    ps.setString(2, monstruo.getHabitat());
    ps.setString(3, monstruo.getEstado());
    ps.setString(4, monstruo.getColaCercenable());
    ps.setString(5, monstruo.getDragonAnciano());
    ps.setString(6, monstruo.getDebilidadFuego());
    ps.setString(7, monstruo.getDebilidadAgua());
    ps.setString(8, monstruo.getDebilidadRayo());
    ps.setString(9, monstruo.getDebilidadHielo());
    ps.setString(10, monstruo.getDebilidadDraco());

    // upload the data, not the toString result of the array
    ps.setBlob(11, new SerialBlob(blob));

    ps.executeUpdate();
}


来源:https://stackoverflow.com/questions/53365680/uploading-an-image-to-a-database-with-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!