Boa noite Galerinha!!
Tentei achar alguns exemplos na net, mas até agora nada.
Tenho uma função no programa para selecionar uma imagem pelo JFileChooser. Onde ao clicar no botão ele abre o o painel para escolher a imagem, jogando a mesma em um JLabel.
Agora quero gravar essa imagem no banco. @-@"
BOTÃO SALVAR
[code]
private void BT_SalvarMouseClicked(java.awt.event.MouseEvent evt) {
//Criando um acesso ao Bando no botão SALVAR.
if (TF_Cel.getText().trim().isEmpty()){
try
{
String sqllinsert = (String) ("insert into CLIENTE (nome,cpf,idade,"
+ "codbarras,telres,presenca,foto) values (?,?,?,?,?,?,?)");
PreparedStatement stat = con_Cadastro.conexao.prepareStatement(sqllinsert);
stat.setString(1, TF_Nome.getText());
stat.setString(2, TF_CPF.getText());
stat.setInt(3, new Integer(TF_Idade.getText()));
stat.setInt(4, new Integer(TF_Cod.getText()));
stat.setInt(5, new Integer(TF_Res.getText()));
stat.setString (6, new Boolean(chk_Presenca.isSelected()).toString());
//???
System.out.println(sqllinsert);
stat.execute();
JOptionPane.showMessageDialog(null,"Cadastro realizado com sucesso.");
con_Cadastro.ExecutarSQL("select * from CLIENTE");
TF_Pesquisa.setText(null);
TF_Nome.setText(null);
TF_Idade.setText(null);
TF_CPF.setText(null);
TF_Cod.setText(null);
TF_Res.setText(null);
TF_Cel.setText(null);
}
catch (SQLException erro)
{
System.out.println("Erro ao gravar registro." + erro);
}
}[/code]
BOTÃO DE ONDE PUXO A IMAGEM.
[code]private void JB_FotoActionPerformed(java.awt.event.ActionEvent evt) {
//Criação do FileChooser
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle(“Importar imagem”);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// int a = fileChooser.showOpenDialog(null);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
File arquivo = fileChooser.getSelectedFile();//arquivo
BufferedImage bi = null;
try {
bi = ImageIO.read(arquivo); //carrega a imagem real num buffer
} catch (IOException ex) {
Logger.getLogger(Cadastro.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedImage aux = new BufferedImage(150, 120, 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) 150 / bi.getWidth(), (double) 120 / bi.getHeight());//cria a transformacao
g.drawRenderedImage(bi, at);//pinta e transforma a imagem real no auxiliar
LB_Foto.setIcon(new ImageIcon(aux));//seta no jlabel
//return;
//LB_Foto.setIcon(new ImageIcon(fileChooser.getSelectedFile().getPath()));
}
//Abre a caixa para escolher a imagem[/code]
Alguma ideia de como fazer isso? :roll: