Estou com um problema de conexão entre o Java e o Access. Estou tentando criar uma tela de JFrame para pegar preços e descrições de certos produtos. Tenho minha base de dados salva no Access, e criei uma conexão OBDC para que o Java conseguisse entender.
Segue o código que desenvolvi.
package swing;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
public class TelaProjetoBazar extends javax.swing.JFrame {
java.sql.Connection conexao;
java.sql.Statement comando;
java.sql.ResultSet rsRegistro;
private String strEAN;
public TelaProjetoBazar() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnApagar = new javax.swing.JButton();
btnProximo = new javax.swing.JButton();
btnFinalizar = new javax.swing.JButton();
txtEAN = new javax.swing.JTextField();
txtQtd = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
lblDescricao = new javax.swing.JLabel();
lblMarca = new javax.swing.JLabel();
lblPreco = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
lblQtdTotal = new javax.swing.JLabel();
lblPrecoTotal = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
txtCPF = new javax.swing.JTextField();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
btnConsultar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnApagar.setText("Apagar");
btnApagar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnApagarActionPerformed(evt);
}
});
btnProximo.setText("Próximo");
btnProximo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnProximoActionPerformed(evt);
}
});
btnFinalizar.setText("Finalizar");
btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFinalizarActionPerformed(evt);
}
});
jLabel1.setText("EAN");
jLabel2.setText("Quantidade");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel3.setText("Descrição");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("Marca");
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Preço");
lblDescricao.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblDescricao.setText("-");
lblMarca.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblMarca.setText("-");
lblPreco.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblPreco.setText("-");
jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel9.setForeground(new java.awt.Color(255, 0, 0));
jLabel9.setText("Quantidade total");
jLabel10.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel10.setForeground(new java.awt.Color(255, 0, 0));
jLabel10.setText("Preço total");
lblQtdTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblQtdTotal.setForeground(new java.awt.Color(255, 0, 0));
lblQtdTotal.setText("-");
lblPrecoTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
lblPrecoTotal.setForeground(new java.awt.Color(255, 0, 0));
lblPrecoTotal.setText("-");
jLabel13.setText("Nome");
jLabel14.setText("CPF");
btnConsultar.setText("Consultar");
btnConsultar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConsultarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel10)
.addComponent(jLabel9))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblQtdTotal)
.addComponent(lblPrecoTotal)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblPreco)
.addComponent(lblMarca)
.addComponent(lblDescricao)))
.addGroup(layout.createSequentialGroup()
.addComponent(btnApagar)
.addGap(26, 26, 26)
.addComponent(btnProximo)
.addGap(27, 27, 27)
.addComponent(btnFinalizar))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtEAN, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
.addComponent(jLabel1)
.addComponent(jLabel13)
.addComponent(txtNome))
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel14)
.addComponent(jLabel2)
.addComponent(txtQtd, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
.addComponent(txtCPF))
.addGap(36, 36, 36)
.addComponent(btnConsultar)))
.addContainerGap(33, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel13)
.addComponent(jLabel14))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtEAN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtQtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnConsultar))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(lblDescricao))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(lblMarca))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(lblPreco))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnApagar)
.addComponent(btnProximo)
.addComponent(btnFinalizar))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(lblQtdTotal))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel10)
.addComponent(lblPrecoTotal))
.addContainerGap())
);
pack();
}// </editor-fold>
private void btnApagarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
} catch (Exception Excecao) {
JOptionPane.showMessageDialog(null, "SQLException " + Excecao.getMessage(), "Erro: carga do banco de dados ", JOptionPane.INFORMATION_MESSAGE);
}
try {
conexao = DriverManager.getConnection("jdbc:odbc:Estoque", "", "");
comando = conexao.createStatement();
} catch (Exception Excecao) {
JOptionPane.showMessageDialog(null, "SQLException " + Excecao.getMessage(), "Erro: conexão do banco de dados ", JOptionPane.INFORMATION_MESSAGE);
}
try {
PreparedStatement strComandoSQL;
strComandoSQL = conexao.prepareStatement("SELECT * FROM Estoque WHERE Ean = " + strEAN);
rsRegistro = strComandoSQL.executeQuery();
if (rsRegistro.next()) {
lblDescricao.setText(rsRegistro.getString("Descricao"));
} else {
lblDescricao.setText("Valor não encontrado");
}
comando.close();
conexao.close();
} catch (Exception Excecao) {
JOptionPane.showMessageDialog(null, "SQLException " + Excecao.getMessage(), "Erro: Seleção de registro ", JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TelaProjetoBazar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaProjetoBazar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaProjetoBazar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaProjetoBazar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaProjetoBazar().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnApagar;
private javax.swing.JButton btnConsultar;
private javax.swing.JButton btnFinalizar;
private javax.swing.JButton btnProximo;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel9;
private javax.swing.JLabel lblDescricao;
private javax.swing.JLabel lblMarca;
private javax.swing.JLabel lblPreco;
private javax.swing.JLabel lblPrecoTotal;
private javax.swing.JLabel lblQtdTotal;
private javax.swing.JTextField txtCPF;
private javax.swing.JTextField txtEAN;
private javax.swing.JTextField txtNome;
private javax.swing.JTextField txtQtd;
// End of variables declaration
}
Sempre que tento rodar o programa ele me apresenta as mensagens de “Erro: carga do banco de dados” - SQLEXCEPTION`` sun.jdbc.odbc.JdbcOdbcDriver e
“Erro: conexão com o banco de dados” - No suitable driver found for jdbc.odbc:Estoque.
Poderiam me ajudar a encontrar meu erro nesse código.