Ola galera estou com dificuldades para autenticar usuario e senha salvos no banco de dados, ele ate faz a autenticação o problema é que ele só reconhece a primeira linha da tabela por causa do comando conecta.rs.first().
Como faço pra poder acessar com todos os usuarios salvos no banco de dados?
package visao;
import controle.ConexaoBD;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
-
@author pedro
*/
public class TelaLogin extends javax.swing.JFrame {/**
- Creates new form TelaLogin
*/
ConexaoBD conecta = new ConexaoBD();
public TelaLogin() {
initComponents();
conecta.conexao();}
public void acessarSistema(){
try { conecta.stmt = conecta.con.createStatement(); String sql = "SELECT user, senha FROM logins "; conecta.rs = conecta.stmt.executeQuery(sql); conecta.rs.first(); if(jTextFieldUsuario.getText().equals(conecta.rs.getString("user"))&&jTextFieldSenha.getText().equals(conecta.rs.getString("senha"))){ JOptionPane.showMessageDialog(null, "Logado com sucesso!\n"); TelaPrincipal tela = new TelaPrincipal(); tela.setVisible(true); dispose(); }else{ JOptionPane.showMessageDialog(null, "Erro ao logar!\n"); jTextFieldUsuario.setText(null); jTextFieldSenha.setText(null); } } catch (SQLException ex) { Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex); }}
/**
- This method is called from within the constructor to initialize the form.
- WARNING: Do NOT modify this code. The content of this method is always
- regenerated by the Form Editor.
*/
//
private void initComponents() {jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButtonEntrar = new javax.swing.JButton(); jTextFieldUsuario = new javax.swing.JTextField(); jTextFieldSenha = new javax.swing.JTextField(); jButtonSair = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Usuário:"); jLabel2.setText("Senha:"); jButtonEntrar.setText("Entrar"); jButtonEntrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonEntrarActionPerformed(evt); } }); jButtonSair.setText("Sair"); jButtonSair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSairActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextFieldSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addComponent(jButtonEntrar) .addGap(18, 18, 18) .addComponent(jButtonSair))) .addContainerGap(102, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(42, 42, 42) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextFieldUsuario, 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.BASELINE) .addComponent(jLabel2) .addComponent(jTextFieldSenha, 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.BASELINE) .addComponent(jButtonEntrar) .addComponent(jButtonSair)) .addContainerGap(44, Short.MAX_VALUE)) ); setSize(new java.awt.Dimension(373, 215)); setLocationRelativeTo(null);}//
private void jButtonEntrarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:acessarSistema();}
private void jButtonSairActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
conecta.desconecta();
System.exit(0);
}/**
-
@param args the command line arguments
/
public static void main(String args[]) {
/ Set the Nimbus look and feel /
//
/ 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(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TelaLogin().setVisible(true);
}
});
} - For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
// Variables declaration - do not modify
private javax.swing.JButton jButtonEntrar;
private javax.swing.JButton jButtonSair;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextFieldSenha;
private javax.swing.JTextField jTextFieldUsuario;
// End of variables declaration
} - Creates new form TelaLogin