No caso do meu código.
Segue o VIEW.cadAnexo
package VIEW;
import MODEL.BEAN.Anexo;
import MODEL.DAO.AnexoDAO;
import java.awt.Color;
import java.awt.Toolkit;
import java.io.File;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class cadAnexo extends javax.swing.JFrame {
public cadAnexo(){
initComponents();
setIcon();
LimparCampos();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel21 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
jBAbrir = new javax.swing.JButton();
jBExcluir = new javax.swing.JButton();
jBSair = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTAnexos = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
jTFCaminho = new javax.swing.JTextField();
jBAnexar = new javax.swing.JButton();
jTFNome = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jBSalvar = new javax.swing.JButton();
jBNovo = new javax.swing.JButton();
jLNomeFuncionario = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
jLabel21.setText("©2017, Copyright by W.A.R.V. Soft Developers");
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jPanel1.setEnabled(false);
jBAbrir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RESOURCES/abrir.png"))); // NOI18N
jBAbrir.setText("Abrir");
jBAbrir.setEnabled(false);
jBExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RESOURCES/excluir.png"))); // NOI18N
jBExcluir.setText("Excluir");
jBExcluir.setEnabled(false);
jBSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RESOURCES/sair.png"))); // NOI18N
jBSair.setText("Sair");
jBSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSairActionPerformed(evt);
}
});
jTAnexos.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"COD", "Nome", "Arquivo", "Caminho"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTAnexos);
jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jTFCaminho.setEditable(false);
jTFCaminho.setText("Caminho");
jTFCaminho.setEnabled(false);
jBAnexar.setText("Anexar arquivo");
jBAnexar.setEnabled(false);
jBAnexar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBAnexarActionPerformed(evt);
}
});
jTFNome.setText("Nome");
jTFNome.setEnabled(false);
jLabel1.setText("Nome:");
jLabel2.setText("Caminho:");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFNome, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTFCaminho, javax.swing.GroupLayout.PREFERRED_SIZE, 471, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBAnexar)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTFNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jTFCaminho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jBAnexar))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jBSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RESOURCES/adicionar.png"))); // NOI18N
jBSalvar.setText("Salvar");
jBSalvar.setEnabled(false);
jBSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBSalvarActionPerformed(evt);
}
});
jBNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/RESOURCES/novo.png"))); // NOI18N
jBNovo.setText("Novo");
jBNovo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBNovoActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jBNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jBSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jBAbrir, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jBExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jBSair, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jBAbrir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jBNovo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jLNomeFuncionario.setText("Usuário");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel21)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLNomeFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel21)
.addComponent(jLNomeFuncionario))
.addContainerGap())
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void jBAnexarActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Selecionar arquivo");
fileChooser.showOpenDialog(this);
int retorno = fileChooser.showOpenDialog(this);
if(retorno == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
jTFCaminho.setText(file.getPath());
}
jBSalvar.setEnabled(true);
jTFNome.setEnabled(true);
}
private void jBSairActionPerformed(java.awt.event.ActionEvent evt) {
telaPrincipal tela = new telaPrincipal();
tela.setVisible(true);
dispose();
}
private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {
if(jTFNome.getText().isEmpty()){
jTFNome.setText("Por favor insira um nome!");
jTFNome.setForeground(Color.red);
} else {
File arquivo = new File("CAMINHO_DO_ARQUIVO");
FileInputStream fis;
try {
fis = new FileInputStream(arquivo);
} catch (FileNotFoundException ex) {
Logger.getLogger(cadAnexo.class.getName()).log(Level.SEVERE, null, ex);
}
//o campo ARQ é do tipo BLOB
/*PreparedStatement ps = conn.prepareStatement("INSERT INTO ARQUIVO (arq) VALUES (?)");
ps.setBinaryStream(l, fis, (int)arquivo.length());
ps.execute();*/
}
jBNovo.setEnabled(true);
}
private void jBNovoActionPerformed(java.awt.event.ActionEvent evt) {
jBAnexar.setEnabled(true);
jBNovo.setEnabled(false);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cadAnexo().setVisible(true);
}
});
}
Anexo ane = new Anexo();
AnexoDAO daoAne = new AnexoDAO();
// Variables declaration - do not modify
private javax.swing.JButton jBAbrir;
private javax.swing.JButton jBAnexar;
private javax.swing.JButton jBExcluir;
private javax.swing.JButton jBNovo;
private javax.swing.JButton jBSair;
private javax.swing.JButton jBSalvar;
private javax.swing.JLabel jLNomeFuncionario;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel21;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTAnexos;
private javax.swing.JTextField jTFCaminho;
private javax.swing.JTextField jTFNome;
// End of variables declaration
private void setIcon() {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Logo.JPG")));
System.getProperty("login", "");
}
private void LimparCampos() {
jTFNome.setText("");
jTFCaminho.setText("");
}
}
O MODEL.DAO.AnexoDAO:
package MODEL.DAO;
import java.io.File;
import MODEL.BEAN.Anexo;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class AnexoDAO extends Model.dao.ConexaoDAO{
public void inserir( Anexo anexo ){
conectar();
String sql = "INSERT INTO ANEXO (ANE_NOME, ANE_ARQUIVO, ANE_TIPO, ANE_CAMINHO) "
+ "VALUES ('"+anexo.getNome()+"','"+anexo.getArquivo()
+"','"+anexo.getTipo()+"','"+anexo.getCaminho()+"')";
try{
comando.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"Arquivo salvo com sucesso!","Mensagem do Sistema",1);
}catch(SQLException e){
imprimeErro("Erro ao salvar arquivo!", e.getMessage());
}finally{
fechar();
}
}
public Anexo buscarArquivo( String nome ){
conectar();
Anexo ane = new Anexo();
ResultSet rs;
try{
rs = comando.executeQuery(
"SELECT * FROM ANEXO "
+ "WHERE ANE_NOME '"+ nome
+ "%'OR ANE_ID LIKE '" + nome
+ " 'OR ANE_CAMINHO LIKE '" + nome
+ " 'OR ANE_TIPO LIKE '" + nome
+ " 'OR ANE_ARQUIVO LIKE '" + nome + "';");
while (rs.next()){
ane.setCodigo (rs.getInt("ANE_ID"));
ane.setNome (rs.getString("ANE_NOME"));
ane.setArquivo(rs.getBytes("ANE_ARQUIVO"));
ane.setTipo (rs.getString("ANE_TIPO"));
ane.setCaminho(rs.getString("ANE_CAMINHO"));
}
fechar();
return ane;
} catch (SQLException e){
imprimeErro("Erro ao buscar o arquivo!", e.getMessage());
return null;
}
}
public void deletar (Anexo ane){
conectar();
String sql = "DELETE FROM ANEXO WHERE ANE_ID ='" + ane.getCodigo()+"';";
try {
comando.executeUpdate(sql);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Erro ao apagar o arquivo!",
"Mensagem do sistema",0);
}finally{
fechar();
}
}
}
E o MODEL.BEAN.ANEXO:
package MODEL.BEAN;
public class Anexo {
int Codigo;
String Nome;
byte[] Arquivo;
String Tipo;
String Caminho;
public int getCodigo() {
return Codigo;
}
public void setCodigo(int Codigo) {
this.Codigo = Codigo;
}
public String getNome() {
return Nome;
}
public void setNome(String Nome) {
this.Nome = Nome;
}
public byte[] getArquivo() {
return Arquivo;
}
public void setArquivo(byte[] Arquivo) {
this.Arquivo = Arquivo;
}
public String getTipo() {
return Tipo;
}
public void setTipo(String TIpo) {
this.Tipo = Tipo;
}
public String getCaminho() {
return Caminho;
}
public void setCaminho(String Caminho) {
this.Caminho = Caminho;
}
}
Obrigado!