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?
– Danilo Gomes
And that ta show no error, simply did not insert.
– Gleiston Jose de Santana
You can log in or 'spit' this Exception somewhere, ease the way for solving the problem "catch (Ioexception ex) { do something here }"
– Danilo Gomes
@Gleistonjosedesantana change
catch (IOException ex) { }
forcatch (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.– Tiago Ferezin