Autor
|
Tema: Validar usuario y contraseña (Leído 21,391 veces)
|
mapers
Desconectado
Mensajes: 638
|
Buenas señores estoy empenzando a programar en mysql conexion java hize que conectara mi base de datoas pero el problema que tengo es que necesito validar el usuario y contraseña con una ventana aparte esa parte no se como hacerla me podrian ayudar...........hay les dejo mi codigo import java.sql.*; import javax.swing.JOptionPane; public class BDManagent { private final static String drv = "com.mysql.jdbc.Driver"; private final static String db = "jdbc:mysql://127.0.0.1:3306/mapers"; private final static String usar = "root"; private final static String Pass = "mapers"; public BDManagent() { try { Class.forName(drv); st = ct.createStatement(); JOptionPane. showMessageDialog(null, "Me conecte !!!!"); System. out. println("No se pudo conectar con la base de datos"); } } public void showAll() { try { ResultSet rs = st. executeQuery("SELECT * FROM mapers"); while (rs.next()) { System. out. println(rs. getString(1) + " | " + rs. getString(2)+ " | " + rs. getString(3)); } System. out. println("Error al realizar la consulta"); } } return st; } }
|
|
|
En línea
|
|
|
|
1mpuls0
Desconectado
Mensajes: 1.186
|
Te ayudaría... pero nunca he mirado que lo pidas de favor o des las gracias, inclusive ya ni eso.. tampoco mencionas si las soluciones con las que se te ayudan resolvieron tu duda o problema.
Saludos.
|
|
|
En línea
|
abc
|
|
|
mapers
Desconectado
Mensajes: 638
|
siempre doy las gracias :S no soy ingrato
|
|
|
En línea
|
|
|
|
Arobavx
|
pon la estructura de tu tabla.. Saludos
|
|
|
En línea
|
|
|
|
hack-4-life
|
hola mennn..si no es muy tarde..te djo esto..se que te servira yo pase por eso tambien pidiendo ayuda y ahora me toca ayudar es un jframe cambia los valores por los tuyos..si tienes dudas preguntalas men saludos package accesosimple; import javax.swing.*; import javax.swing.*; import javax.swing.*; import java.io.*; import java.sql.*; import java.awt.Panel.*; import java.awt.*; import java.awt.event.*; public class ingreso extends javax. swing. JFrame { public ingreso() { super("INGRESO-VALIDACION DE USUARIO"); initComponents(); setTitle("Autentificacion de usuarios"); setSize(500,390); // Tamanio del Frame setResizable(false); // que no se le pueda cambiar el tamanio //Centrar la ventana de autentificacion en la pantalla Dimension tamFrame =this. getSize();//para obtener las dimensiones del frame Dimension tamPantalla =Toolkit. getDefaultToolkit(). getScreenSize(); //para obtener el tamanio de la pantalla setLocation((tamPantalla.width-tamFrame.width)/2, (tamPantalla.height-tamFrame.height)/2); //para posicionar setVisible(true); // Hacer visible al frame } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jpnl_login = new javax. swing. JPanel(); btnlogin = new javax. swing. JButton(); btncancelar = new javax. swing. JButton(); lblusuario = new javax. swing. JLabel(); lblacceso = new javax. swing. JLabel(); lblpasswd = new javax. swing. JLabel(); txtUser = new javax.swing.JFormattedTextField(); setName("login"); // NOI18N jpnl_login. setBorder(javax. swing. BorderFactory. createTitledBorder("VALIDACION DE USUARIO")); btnlogin.setText("Login"); public void actionPerformed (java. awt. event. ActionEvent evt ) { btnloginActionPerformed(evt); } }); btncancelar.setText("Cancelar"); public void actionPerformed (java. awt. event. ActionEvent evt ) { btncancelarActionPerformed(evt); } }); lblusuario.setText("Usuario:"); lblacceso. setFont(new java. awt. Font("Trebuchet MS", 1, 12)); lblacceso. setForeground(new java. awt. Color(255, 102, 102)); lblacceso.setText("ACCESO-VALIDACION DE USUARIO"); lblpasswd.setText("Password:"); public void actionPerformed (java. awt. event. ActionEvent evt ) { txtUserActionPerformed(evt); } }); javax.swing.GroupLayout jpnl_loginLayout = new javax.swing.GroupLayout(jpnl_login); jpnl_login.setLayout(jpnl_loginLayout); jpnl_loginLayout.setHorizontalGroup( jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jpnl_loginLayout.createSequentialGroup() .addContainerGap() .addComponent(btnlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) . addPreferredGap(javax. swing. LayoutStyle. ComponentPlacement. RELATED, 140, Short. MAX_VALUE) .addComponent(btncancelar) .addGap(89, 89, 89)) .addGroup(jpnl_loginLayout.createSequentialGroup() .addGroup(jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpnl_loginLayout.createSequentialGroup() .addComponent(lblusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtUser)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpnl_loginLayout.createSequentialGroup() .addComponent(lblpasswd, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(209, 209, 209)) .addGroup(jpnl_loginLayout.createSequentialGroup() .addGap(47, 47, 47) .addComponent(lblacceso, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(128, Short. MAX_VALUE)) ); jpnl_loginLayout.setVerticalGroup( jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jpnl_loginLayout.createSequentialGroup() .addGap(16, 16, 16) .addComponent(lblacceso) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jpnl_loginLayout.createSequentialGroup() .addGap(28, 28, 28) .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(36, 36, 36) .addGroup(jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblpasswd, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) . addPreferredGap(javax. swing. LayoutStyle. ComponentPlacement. RELATED, 80, Short. MAX_VALUE) .addGroup(jpnl_loginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnlogin) .addComponent(btncancelar)) .addGap(25, 25, 25)) ); 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(37, 37, 37) .addComponent(jpnl_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(24, Short. MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jpnl_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) ); pack(); }// </editor-fold> private void txtUserActionPerformed (java. awt. event. ActionEvent evt ) { } private void btnloginActionPerformed (java. awt. event. ActionEvent evt ) { ingreso ap=new ingreso(); ap.setVisible(false); try { //chekar si el usuario escrbio el nombre de usuario y pw if (txtUser.getText().length() > 0 && txtPass.getText().length() > 0 ) { // Si el usuario si fue validado correctamente if( validarUsuario( txtUser.getText(), txtPass.getText() ) ) //enviar datos a validar { int answer = JOptionPane. showConfirmDialog(null, "BIENVENIDO DESEA CONTINUAR", "ACCCESO", JOptionPane. YES_OPTION); { setVisible(false); principal start = new principal(); start.show(); } // Codigo para mostrar la ventana principal setVisible(false); //VentanaPrincipal ventana1 = new VentanaPrincipal(); //ventana1.show(); } else { JOptionPane. showMessageDialog(null, "El nombre de usuario y/o contrasenia no son validos."); JOptionPane. showMessageDialog(null, txtUser. getText()+" " +txtPass. getText() ); txtUser.setText(""); //limpiar campos txtPass.setText(""); txtUser.requestFocusInWindow(); } } else { JOptionPane. showMessageDialog(null, "Debe escribir nombre de usuario y contrasenia.\n" + "NO puede dejar ningun campo vacio"); } } { e.printStackTrace(); } } private void btncancelarActionPerformed (java. awt. event. ActionEvent evt ) { // String message = "REALMENTE DESEA SALIR DEL SISTEMA"; String title = "Acceso Al Sistema"; { System. exit(0); // clicked yes } { // clicked no } } { try { //nombre de la BD: bdlogin //nombre de la tabla: usuarios // id integer auto_increment not null <--llave primaria // campos: usuario char(25) // password char(50) Class.forName("com.mysql.jdbc.Driver"); // Preparamos la consulta Statement instruccionSQL = unaConexion. createStatement(); ResultSet resultadosConsulta = instruccionSQL. executeQuery ("SELECT * FROM usuario WHERE nombre='"+elUsr +"' AND password='"+ elPw +"'"); if( resultadosConsulta.first() ) // si es valido el primer reg. hay una fila, tons el usuario y su pw existen return true; //usuario validado correctamente else return false; //usuario validado incorrectamente } { e.printStackTrace(); return false; } } public static void main (String args []) { public void run() { new ingreso().setVisible(true); } }); } // Variables declaration - do not modify private javax. swing. JButton btncancelar ; private javax. swing. JButton btnlogin ; private javax. swing. JPanel jpnl_login ; private javax. swing. JLabel lblacceso ; private javax. swing. JLabel lblpasswd ; private javax. swing. JLabel lblusuario ; private javax.swing.JFormattedTextField txtUser; // End of variables declaration }
|
|
« Última modificación: 13 Mayo 2011, 17:51 pm por hack-4-life »
|
En línea
|
""DE QUE SIRVE EL CONOCIMIENTO SI SE PRIVA DE SU DISTRIBUCION"
|
|
|
mapers
Desconectado
Mensajes: 638
|
Gracias men me sirvio
|
|
|
En línea
|
|
|
|
juanunivalle
Desconectado
Mensajes: 1
|
hack-4-life Muchas Gracias viejo!!!!!!!!
|
|
|
En línea
|
|
|
|
davp
Desconectado
Mensajes: 2
|
Hola soy nuevo en sto pero tal vez sta solucion te ayude... es algo simple..!! package Login; import java.sql.*; import javax.swing.JOptionPane; public class FromUsuario extends javax. swing. JFrame { Usuarios usuClase= new Usuarios(); /** Creates new form FromUsuario */ public FromUsuario() { initComponents(); setLocation(300,240); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax. swing. JPanel(); jPanel2 = new javax. swing. JPanel(); btnIngresar = new javax. swing. JButton(); btnSalir = new javax. swing. JButton(); setTitle("Login"); setResizable(false); jPanel1. setBorder(javax. swing. BorderFactory. createTitledBorder("Usuario")); 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() .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) ); jPanel2. setBorder(javax. swing. BorderFactory. createTitledBorder("Contraseña")); 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(txtPassword, javax. swing. GroupLayout. DEFAULT_SIZE, 142, Short. MAX_VALUE) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) . addContainerGap(javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) ); btnIngresar. setIcon(new javax. swing. ImageIcon("C:\\Users\\user\\Desktop\\Iconos\\icons\\16\\065.png")); // NOI18N btnIngresar.setText("Ingresar"); public void actionPerformed (java. awt. event. ActionEvent evt ) { btnIngresarActionPerformed(evt); } }); btnSalir. setIcon(new javax. swing. ImageIcon("C:\\Users\\user\\Desktop\\Iconos\\icons\\16\\101.png")); // NOI18N btnSalir.setText("Salir"); public void actionPerformed (java. awt. event. ActionEvent evt ) { btnSalirActionPerformed(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(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) . addComponent(jPanel1, javax. swing. GroupLayout. Alignment. LEADING, javax. swing. GroupLayout. DEFAULT_SIZE, javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE) . addComponent(jPanel2, javax. swing. GroupLayout. Alignment. LEADING, javax. swing. GroupLayout. DEFAULT_SIZE, javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) . addComponent(btnIngresar, javax. swing. GroupLayout. DEFAULT_SIZE, javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE) . addComponent(btnSalir, javax. swing. GroupLayout. DEFAULT_SIZE, 93, Short. MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(29, 29, 29) .addComponent(btnIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))) . addContainerGap(javax. swing. GroupLayout. DEFAULT_SIZE, Short. MAX_VALUE)) ); pack(); }// </editor-fold> private void btnIngresarActionPerformed (java. awt. event. ActionEvent evt ) { // TODO add your handling code here: usuClase.setUsuario(txtUsuario.getText()); usuClase.setClave(txtPassword.getText()); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); st=cn.createStatement(); rs=st.executeQuery("select * from usuarios " + "where usuario='"+usuClase.getUsuario()+"' and " + " clave='"+usuClase.getClave()+"'"); if (rs.next()) { JOptionPane. showMessageDialog(null, "El Usuario si existe..!!"); } else { JOptionPane. showMessageDialog(null, "Verifique Usuario oh Password..!!"); } } { System. out. println("Error de clase : "+e ); } { System. out. println("Error de conexión : "+e ); } } private void btnSalirActionPerformed (java. awt. event. ActionEvent evt ) { // TODO add your handling code here: int rsp; rsp =JOptionPane. showConfirmDialog(null, "Desea salir de la Aplicacion ..!!", "Salir", JOptionPane. YES_NO_OPTION); { dispose(); } else { txtUsuario.setText(""); txtPassword.setText(""); } } /** * @param args the command line arguments */ public static void main (String args []) { { public void run() { new FromUsuario().setVisible(true); } }); } // Variables declaration - do not modify private javax. swing. JButton btnIngresar ; private javax. swing. JButton btnSalir ; private javax. swing. JPanel jPanel1 ; private javax. swing. JPanel jPanel2 ; // End of variables declaration }
Suerte ^.^ Horny3
|
|
|
En línea
|
|
|
|
|
Mensajes similares |
|
Asunto |
Iniciado por |
Respuestas |
Vistas |
Último mensaje |
|
|
Validar el nombre de un usuario y contraseña atravez de una base de acces
.NET (C#, VB.NET, ASP)
|
wannlax
|
2
|
8,689
|
10 Julio 2009, 20:44 pm
por wannlax
|
|
|
Validar contraseña c++
Programación C/C++
|
noob2012
|
9
|
14,169
|
1 Junio 2013, 16:22 pm
por noob2012
|
|
|
Duda en HTML - ¿Cómo validar Usuario y Contraseña al mismo tiempo?
Dudas Generales
|
Sazham
|
8
|
20,117
|
23 Agosto 2013, 10:53 am
por Stakewinner00
|
|
|
[ASP.NET] problema al validar Contraseña
.NET (C#, VB.NET, ASP)
|
larezaka
|
0
|
2,111
|
17 Octubre 2014, 03:02 am
por larezaka
|
|
|
jsp servlet validar usuario
Desarrollo Web
|
lucke
|
0
|
3,385
|
5 Mayo 2016, 14:49 pm
por lucke
|
|