Java - Limitar a 3 numero de intentos

 
Vista:

Limitar a 3 numero de intentos

Publicado por Alexzander (1 intervención) el 06/10/2023 21:42:47
Buenas tardes, alguien me podría ayudar en como puedo limitar a 3 números de intentos, he visto muchos ejemplos pero me sale error. Por favor ayúdenme. Este es mi código.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Vistas;

import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
*
* @author Computer
*/
public final class Logueo extends javax.swing.JFrame {

/**
* Creates new form Logueo
*/
public Logueo() {
initComponents();
this.setLocationRelativeTo(null);
Conectar();
}

public Connection Conectar() {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/projecttardanza","root","");
} catch (SQLException e) {
System.err.print(e.toString());
JOptionPane.showMessageDialog(this, "Ocurrio un error inesperado.\nFavor comunicarse con el administrador.");
}
return con;
}

public void ingresar() {
Connection con1 = null;
PreparedStatement pst = null;
ResultSet rs = null;
String User = txtUsuario.getText();
String Pass = txtContra.getText();

if (User.equals("") || Pass.equals("")) {
JOptionPane.showMessageDialog(this, "Uno o mas campos estan vacios. Favor de llenarlos.");
} else {
try {
con1 = Conectar();
pst = con1.prepareStatement("select usuario, contraseña from datos where usuario='" + User
+ "' and contraseña ='" + Pass + "'");
rs = pst.executeQuery();
if (rs.next()) {
this.dispose();
new Menuprincipal().setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "Credenciales incorrectas. Vuelve a intentar de nuevo.");
}
} catch (SQLException e) {
System.err.print(e.toString());
JOptionPane.showMessageDialog(this, "Ocurrio un error inesperado.\nFavor comunicarse con el administrador.");
}
}
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtUsuario = new javax.swing.JTextField();
txtContra = new javax.swing.JPasswordField();
btnIngresar = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Usuario:");

jLabel2.setText("Contraseña:");

btnIngresar.setText("Ingresar");
btnIngresar.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnIngresarMouseClicked(evt);
}
});
btnIngresar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnIngresarActionPerformed(evt);
}
});

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel3.setText("Bienvenido Usuario");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(btnIngresar)
.addGap(88, 88, 88))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
.addComponent(txtContra)))
.addGroup(layout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(jLabel3)))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jLabel3)
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtContra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(49, 49, 49)
.addComponent(btnIngresar)
.addContainerGap(64, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void btnIngresarMouseClicked(java.awt.event.MouseEvent evt) {
if(MouseEvent.BUTTON1 == evt.getButton()){
ingresar();
}
}

private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @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(Logueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Logueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Logueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Logueo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Logueo().setVisible(true);
}
});
}

// Variables declaration - do not modify
public javax.swing.JButton btnIngresar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
public javax.swing.JPasswordField txtContra;
public javax.swing.JTextField txtUsuario;
// End of variables declaration
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Limitar a 3 numero de intentos

Publicado por Maverick (109 intervenciones) el 11/01/2024 21:30:08
Estimado

Buenas tardes, revisando tu código solo veo que estas validando que tanto el User y Pass tenga un valor pero no haz declarado una variable que permita contabilizar los intentos.

if (User.equals("") || Pass.equals("")) {

Por lo que sugiero que declares la variable de control de intentos y sea parte de tu validación.

Así mismo, seria bueno que mensaje de error te sale en tu programa.

Cualquier cosa no dudes en escribirme a: [email protected]

Saludos cordiales,

Manuel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar