Java Desktop with Mysql Database

Asked

Viewed 310 times

1

I’m trying to write an image in the Mysql database, the table is just below, the code to load the image is soon after, I’m using a button to click on a Jlabel, after that, I use another button to insert the image in the database, below is the code of the Insert, finally I’m not able to write in the database.

Mysql bank

create table imagens(
id int not null auto_increment,
cpf varchar(15) not null,
foto blob null,
primary key (id),
foreign key (cpf)
references funcionario(cpf)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB; 

Here and where I upload the image on Jlabel from a Jfilechooser

JFileChooser fileChooser = new JFileChooser();   //Cria o objeto do tipo Janela JFileChooser
        fileChooser.setDialogTitle("Escolha a Foto");  //Define o título do JFileChooser
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);  //Define que só serão abertos arquivos
        {
            if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                try {
                    File arquivo = fileChooser.getSelectedFile();//arquivo
                    BufferedImage bi = ImageIO.read(arquivo); //carrega a imagem real num buffer
                    BufferedImage aux = new BufferedImage(100, 80, bi.getType());//cria um buffer auxiliar com o tamanho desejado
                    Graphics2D g = aux.createGraphics();//pega a classe graphics do aux para edicao
                    AffineTransform at = AffineTransform.getScaleInstance((double) 100 / bi.getWidth(), (double) 80 / bi.getHeight());//cria a transformacao
                    g.drawRenderedImage(bi, at);//pinta e transforma a imagem real no auxiliar
                    foto.setIcon(new ImageIcon(aux));//seta no jlabel
                    foto.setText(null);

                   // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    //Jogue a imagem lá dentro do byteArrayOutputStream
                    //ImageIO.write(aux, "png", byteArrayOutputStream);

                }catch (IOException ex) {

                }


            }
    }

This is my Internet.

String prep = "INSERT INTO imagens (foto,cpf)  VALUES(?,?)";

Image image = ((ImageIcon)foto.getIcon()).getImage();
PreparedStatement sttmt = con.prepareStatement(prep);
JTextField cpff = cpf;

sttmt.setBytes(1, bytes(image));
sttmt.setString(2, cpf.getText());

sttmt.execute();
sttmt.close();
  • What is the error message?

  • And that ta show no error, simply did not insert.

  • You can log in or 'spit' this Exception somewhere, ease the way for solving the problem "catch (Ioexception ex) { do something here }"

  • @Gleistonjosedesantana change catch (IOException ex) { } for catch (IOException ex) { e.printStackTrace();} and put the error fired, now do not know why you did an Exception catch if not asking to show the error fired.

1 answer

1

Well, it’s not recommended that you put a direct image in the database. because in addition to being unnecessary it would take up a lot of space and make your bank inflate in an exorbitant way.

Try uploading this image somewhere, such as a shared folder on the network or in a cloud service. and in the database you only save the relative path of the image for example:

\\servidor\app\imagens\usuario\perfil.png
ou
https://raw.githubusercontent.com/zerossB/TotalRam/master/Images/TotalRAM-screen.png

There to recover you pass as the path of the image:

ImageIcon logo = new ImageIcon(new URL("https://raw.githubusercontent.com/zerossB/TotalRam/master/Images/TotalRAM-screen.png"));

So your bank will be lighter and less data processing between the app and the database.

  • I liked the idea of shared folder on the network,had not thought on this side,beginner things,good people thank you,I will do the implementation,thank you even for the tips,valeu...

  • Just need to call ;D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.