Estou a Testar um Cadastro de Usuários (Id, Nome e Senha), o programa apresenta os dados nas TextFields e Conecta Normalmente, mas acontece que não consigo operacionar com Elas, vou postar o Código pra melhor entendimento:
Class Conexão:
package move_Computer;
import java.sql.*;
import javax.swing.JOptionPane;
public class ConexaoBD implements Interface{
private String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private String url = "jdbc:odbc:Login";
private String usuário = "";
private String senha = "";
private Connection conexao;
public Statement statement;
public ResultSet resultset;
public boolean conectar()
{
boolean resultado = true;
try
{
Class.forName(driver);
conexao = DriverManager.getConnection(url, usuário, senha);
}
catch(ClassNotFoundException driv)
{
JOptionPane.showMessageDialog(null, "Driver: " + driv.getMessage() + " Não Localizado");
resultado = false;
}
catch(SQLException fonte)
{
JOptionPane.showMessageDialog(null, "Deu Erro na Conexão "+
"com a Fonte de Dados: " + fonte.getMessage());
resultado = false;
}
return resultado;
}
public void desconecta()
{
@SuppressWarnings("unused")
boolean resultado = true;
try
{
conexao.close();
JOptionPane.showMessageDialog(null,"banco fechado");
}
catch(SQLException fecha)
{
JOptionPane.showMessageDialog(null,"Não foi possivel "+
"fechar o banco de dados: "+fecha.getMessage());
resultado = false;
}
}
public void executarSQL(String sql)
{
try
{
statement = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
resultset = statement.executeQuery(sql);
}
catch(SQLException sqlex)
{
JOptionPane.showMessageDialog(null,"Não foi possivel executar o comando sql"+ sqlex +" ,o sql passado foi "+ sql);
}
}
}
Class formulário
package move_Computer;
import java.awt.HeadlessException;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author Sahed Ribeiro
*/
@SuppressWarnings("serial")
public class FrmLogin extends javax.swing.JFrame {
/** Creates new form frmLogin */
public FrmLogin() {
initComponents();
ConexaoBD cad = new ConexaoBD();
cad.conectar();
cad.executarSQL("select * from Login");
try
{
cad.resultset.first();
tf_id.setText(cad.resultset.getString("Código"));
tf_Nome.setText(cad.resultset.getString("Nome"));
tf_Senha.setText(cad.resultset.getString("Senha"));
}
catch(SQLException erro)
{
JOptionPane.showMessageDialog(null,"Não localizou dados "+erro);
}
}
private void mostra_dados() throws SQLException
{
ConexaoBD cad = new ConexaoBD();
tf_id.setText(cad.resultset.getString("Código"));
tf_Nome.setText(cad.resultset.getString("Nome"));
tf_Senha.setText(cad.resultset.getString("Senha"));
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
tf_id = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
tf_Nome = new javax.swing.JTextField();
tf_Senha = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
bt_Add = new javax.swing.JButton();
bt_Seguinte = new javax.swing.JButton();
bt_Anterior = new javax.swing.JButton();
bt_Editar = new javax.swing.JButton();
bt_Deletar = new javax.swing.JButton();
bt_Gravar = new javax.swing.JButton();
bt_Sair = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jToolBar1 = new javax.swing.JToolBar();
bt_Login = new javax.swing.JButton();
jSeparator1 = new javax.swing.JToolBar.Separator();
bt_sair1 = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jFicheiro = new javax.swing.JMenu();
mnu_Sair = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("ID Usuário:");
jLabel2.setText("Nome:");
jLabel3.setText("Senha:");
bt_Add.setText("Adicionar");
bt_Add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_AddActionPerformed(evt);
}
});
bt_Seguinte.setText("Seguinte");
bt_Seguinte.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_AddActionPerformed(evt);
}
});
bt_Anterior.setText("Anterior");
bt_Anterior.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_AddActionPerformed(evt);
}
});
bt_Editar.setText("Editar");
bt_Editar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_EditarActionPerformed(evt);
}
});
bt_Deletar.setText("Eliminar");
bt_Deletar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_DeletarActionPerformed(evt);
}
});
bt_Gravar.setText("Guardar");
bt_Gravar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_GravarActionPerformed(evt);
}
});
bt_Sair.setText("Sair");
bt_Sair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_SairActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel4.setText("Sistema de Cadastro");
jToolBar1.setRollover(true);
bt_Login.setText("Login");
bt_Login.setFocusable(false);
bt_Login.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
bt_Login.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
bt_Login.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt_LoginActionPerformed(evt);
}
});
jToolBar1.add(bt_Login);
jToolBar1.add(jSeparator1);
bt_sair1.setText("Sair");
bt_sair1.setFocusable(false);
bt_sair1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
bt_sair1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(bt_sair1);
jFicheiro.setText("Ficheiro");
mnu_Sair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
mnu_Sair.setIcon(new javax.swing.ImageIcon("C:\Users\Sahed Ribeiro\Downloads\Detecção de colisão entre duas imagens em Java_files\cc_by_sa.png")); // NOI18N
mnu_Sair.setText("Sair");
mnu_Sair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnu_SairActionPerformed(evt);
}
});
jFicheiro.add(mnu_Sair);
jMenuBar1.add(jFicheiro);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(9, 9, 9))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tf_Senha)
.addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(tf_id, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(bt_Add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Editar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Deletar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Gravar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Sair)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Seguinte)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt_Anterior))
.addGroup(layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(jLabel4)))))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addGap(59, 59, 59)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(tf_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(tf_Senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bt_Add)
.addComponent(bt_Editar)
.addComponent(bt_Deletar)
.addComponent(bt_Gravar)
.addComponent(bt_Sair)
.addComponent(bt_Seguinte)
.addComponent(bt_Anterior))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bt_AddActionPerformed(java.awt.event.ActionEvent evt) {
tf_Nome.setText("");
tf_Senha.setText("");
}
@SuppressWarnings("unused")
private void bt_SeguinteActionPerformed(java.awt.event.ActionEvent evt) {
ConexaoBD cad = new ConexaoBD();
try
{
cad.resultset.next();
mostra_dados();
}
catch(SQLException erro)
{
JOptionPane.showMessageDialog(null, "Erro: " + erro.getStackTrace());
}
}
@SuppressWarnings("unused")
private void bt_AnteriorActionPerformed(java.awt.event.ActionEvent evt) {
ConexaoBD cad = new ConexaoBD();
try
{
cad.resultset.previous();
mostra_dados();
}
catch(SQLException erro)
{
JOptionPane.showMessageDialog(null, "Erro: " + erro.getStackTrace());
}
}
private void bt_EditarActionPerformed(java.awt.event.ActionEvent evt) {
}
private void bt_DeletarActionPerformed(java.awt.event.ActionEvent evt) {
}
private void bt_GravarActionPerformed(java.awt.event.ActionEvent evt) {
ConexaoBD cad = new ConexaoBD();
try{
String sql = "INSERT INTO Login (Nome, Senha) VALUES (" + tf_Nome.getText() +
"," + tf_Senha.getText() + ")";
cad.statement.executeQuery(sql);
JOptionPane.showMessageDialog(null, "Dados Salvos com Sucesso");
cad.resultset = cad.statement.executeQuery("SELECT * FROM Login");
cad.resultset.first();
mostra_dados();
}
catch(SQLException erro)
{
JOptionPane.showMessageDialog(null, "Erro: " + erro.getMessage());
}
catch(HeadlessException erro)
{
JOptionPane.showMessageDialog(null, "Erro: " + erro.getMessage());
}
}
private void bt_SairActionPerformed(java.awt.event.ActionEvent evt) {
}
private void bt_LoginActionPerformed(java.awt.event.ActionEvent evt) {
}
private void mnu_SairActionPerformed(java.awt.event.ActionEvent evt) {
}
public void Main() {
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new FrmLogin().setVisible(true);
}
}
);
}
// Variables declaration - do not modify
private javax.swing.JButton bt_Add;
private javax.swing.JButton bt_Deletar;
private javax.swing.JButton bt_Editar;
private javax.swing.JButton bt_Gravar;
private javax.swing.JButton bt_Login;
private javax.swing.JButton bt_Sair;
private javax.swing.JButton bt_sair1;
private javax.swing.JButton bt_Seguinte;
private javax.swing.JButton bt_Anterior;
private javax.swing.JMenu jFicheiro;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JMenuItem mnu_Sair;
private javax.swing.JTextField tf_Nome;
private javax.swing.JTextField tf_Senha;
private javax.swing.JTextField tf_id;
// End of variables declaration
}
Interface
package move_Computer;
public interface Interface {
public boolean conectar();
public void desconecta();
public void executarSQL(String sql);
}
[color=red]Erro Gerado:[/color]
[list]
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at move_Computer.FrmLogin.bt_GravarActionPerformed(FrmLogin.java:300)
at move_Computer.FrmLogin.access$3(FrmLogin.java:295)
at move_Computer.FrmLogin$6.actionPerformed(FrmLogin.java:125)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
[/list]
Alguém Pode Me Dar Uma Ajudinha?! :idea: