Login authentication & mysql

Ads
 

Login authentication & mysql

View Answers

October 6, 2009 at 1:11 PM

Hi Friend,

Login Authentication with MySql using Java Swing:

1)Create a java class CreateAccount.java

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
class CreateAccount extends JFrame{
JTextField text1,text2,text3,text4,text5;
JPasswordField pass1;
JLabel label1,label2,label3,label4,label5,label6;
JPanel panel;
JButton button,button1;
CreateAccount() {
text1=new JTextField(15);
text2=new JTextField(15);
text3=new JTextField(15);
pass1=new JPasswordField(15);
text4=new JTextField(15);
text5=new JTextField(15);
label1=new JLabel("First Name");
label2=new JLabel("Last Name");
label3=new JLabel("User Name");
label4=new JLabel("Password");
label5=new JLabel("Address");
label6=new JLabel("Phone No");
button=new JButton("Save");
button1=new JButton("Exit");
panel=new JPanel(new GridLayout(7,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(pass1);
panel.add(label5);
panel.add(text4);
panel.add(label6);
panel.add(text5);
panel.add(button);
panel.add(button1);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=pass1.getText();
String value5=text4.getText();
String value6=text5.getText();

Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String user1="";
String pass1="";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
int i=st.executeUpdate("insert into login(firstname,lastname,username,password,address,contactno) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"')");
JOptionPane.showMessageDialog(null,"Data is successfully saved.");
}
catch(Exception e){}
}
});
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
setVisible(false);
}
});
add(panel);
setSize(300,400);
}

}

October 6, 2009 at 1:14 PM

continue...

2)Create LoginDemo.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class LoginDemo extends JFrame{
JButton SUBMIT,ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1;
final JPasswordField text2;
LoginDemo(){
label1 = new JLabel();
label1.setText("UserName:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("Login");
ADD=new JButton("Create Account");
panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(SUBMIT);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
String user1="";
String pass1="";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2+"'");
while (res.next()) {
user1 = res.getString("username");
pass1 = res.getString("password");
}
if (value1.equals(user1) && value2.equals(pass1)) {
JOptionPane.showMessageDialog(null,"Welcome "+user1+", You have successfully Login");
}
else{
JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
});
ADD.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
CreateAccount account=new CreateAccount();
account.setVisible(true);
}
});
setTitle("FORM");
}


public static void main(String arg[]) {
LoginDemo frame=new LoginDemo();
frame.setSize(300,100);
frame.setVisible(true);
}
}

If you want the Login application in JSP,visit the following link:

http://www.roseindia.net/jsp/loginbean.shtml

Thanks

May 4, 2013 at 5:40 PM

hey i have taken your code and modified it as below. the problem is i am unable to call my login page from my start page. its not opening the login page when i click on login. can anyone help.. please its urgent..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class Login extends JFrame
{

    JButton SUBMIT,ADD;

    JPanel panel;

    JLabel label1,label2;

    final JTextField text1;

    final JPasswordField text2;

    Login(){

        label1 = new JLabel();

        label1.setText("UserName:");

        text1 = new JTextField(15);

        label2 = new JLabel();

        label2.setText("Password:");

        text2 = new JPasswordField(15);

        SUBMIT=new JButton("Login");

        ADD=new JButton("Create Account");

        panel=new JPanel(new GridLayout(3,2));

        panel.add(label1);

        panel.add(text1);

        panel.add(label2);

        panel.add(text2);

        panel.add(SUBMIT);

        panel.add(ADD);

        add(panel,BorderLayout.CENTER);

        SUBMIT.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){


                String value1=text1.getText();

                String value2=text2.getText();

                  if (value1 == null || value1.equals("")) {
                    JOptionPane.showMessageDialog(null, "Enter user id", "Missing field", JOptionPane.DEFAULT_OPTION);

                    return;
                }
                    if (value2 == null || value2.equals("")) {
                    JOptionPane.showMessageDialog(null, "Enter password", "Missing field", JOptionPane.DEFAULT_OPTION);

                    return;
                }

                Connection con = null;

                String url="jdbc:mysql://localhost:3306/";

                String db="pharma";

                String driver="com.mysql.jdbc.Driver";

                String user="root";

                String pass="root";

                String user1="";

                String pass1="";

                try
                {

                    Class.forName(driver);

                    con = DriverManager.getConnection(url+db, user, pass);

                    Statement st = con.createStatement();

                    ResultSet res = st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2+"'");

                    while (res.next()) {

                        user1 = res.getString("username");

                        pass1 = res.getString("password");

                    }

                    if (value1.equals(user1) && value2.equals(pass1)) {

                        dispose();

                        JOptionPane.showMessageDialog(null,"Welcome "+user1+", You have successfully Login");

                         HcHome h=new HcHome();

            h.setBounds(0,0,1020,1000);

            h.setVisible(true);

                    }

                    else{

                        JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);

                    }

                }

                catch(Exception e){

                    System.out.println(e.getMessage());

                }

            }

        });

        ADD.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){

                CreateAccount account=new CreateAccount();

                account.setVisible(true);

            }

        });

}


public static void main(String arg[]) {
 Login l=new Login();
 l.setBounds(90,150,200,30);
 l.setSize(300,100);
 l.setVisible(true);
  l.setTitle("LOGIN PAGE");
 }
}


here is my start page:-

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class Start extends JFrame 

//implements KeyListener

 {
   JLabel title,title1,title2,line,image;

   JButton Login,Back;

   JPanel p;

   int key;

   String b;

   public Start()

    {

      setLayout(null);

      setTitle("Start");

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      title=new JLabel("WELCOME TO AYURVEDA");

      title.setFont(new Font("arial",Font.BOLD,50));

      title.setForeground(Color.red);

      title.setBounds(200,50,1000,70);

      title1=new JLabel("LUNA PHARMA");

      title1.setFont(new Font("arial",Font.BOLD,40));

      title1.setForeground(Color.RED);

      title1.setBounds(350,100,500,70);

      title2=new JLabel("GMP CERTIFIED UNIT");

      title2.setFont(new Font("arial",Font.BOLD,25));

      title2.setForeground(Color.red);

      title2.setBounds(360,150,500,60);

      line=new JLabel("------------------");

      line.setFont(new Font("arial",Font.BOLD,45));

      line.setForeground(Color.blue);

      line.setBounds(360,200,1000,30);

      getContentPane().add(title);

      getContentPane().add(title1);

      getContentPane().add(title2);

      getContentPane().add(line);

      Login=new JButton("Login");

      Login.setMnemonic(KeyEvent.VK_A);

      Login.setToolTipText("Press it to login");

      Login.setFont(new Font("arial",Font.BOLD,20));

      //Login.addActionListener(this);

     //  Login.addKeyListener(this);

      getContentPane().add(Login);

         Back=new JButton("Exit");

      Back.setMnemonic(KeyEvent.VK_B);

      Back.setToolTipText("Press it to Exit");

      Back.setFont(new Font("arial",Font.BOLD,20));

     // Back.addActionListener(this);

      // Back.addKeyListener(this);

      getContentPane().add(Back);


        p=new JPanel();

      p.setLayout(new GridLayout(1,3));

      p.setBounds(330,650,350,30);

      p.add(Login);

      p.add(Back);

      getContentPane().add(p);

      image=new JLabel(new ImageIcon("images/Front.jpg"));

      image.setBounds(100,250,900,500);

      getContentPane().add(image);

      setSize(1020,1000);

      setVisible(true);


      Back.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){
                   {

                            dispose();

                   }

    }
             });

        Login.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent ae)

    {


        Login l=new Login();

       }
      });

      }


   /*public void keyPressed(KeyEvent ke)

    {

      key=ke.getKeyCode();

       if(key==KeyEvent.VK_A)

        {

          Login l=new Login();

        }
        if(key==KeyEvent.VK_B)

        {

          dispose();

        }



    }*/

   ///public void keyTyped(KeyEvent ke){}

  // public void keyReleased(KeyEvent ke){}

   public static void main(String args[])

    {

      Start sf=new Start();
      sf.setVisible(true);

    }

  }

October 3, 2012 at 4:00 AM

Thanks a lot mugelan........! You are great in such a simple you have written a the above code.....! I understand it very easily.....and i successfully get through the first step of my project. god bless you..!

Ads









Related Tutorials/Questions & Answers:
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
Advertisements
login and registration form using servlet and authentication through mysql
login and registration form using servlet and authentication through mysql ... and authentication through mysql and whenevr i run it on tomcat a blank page is displayed plz... = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost/login
login authentication in jsp and servlet
login authentication in jsp and servlet  Hi, I am beginner in web... to make login authentication in jsp and servlet? Thanks   Hi, I to make login authentication in jsp and servlet you have to write code for database
Struts Login Authentication
Struts Login Authentication  Hi Sir, Am doing a project in a struts 1.2,i want login authentication code fro that, only authenticated user can login into application,i am using back end as Mysql.so send me code as soon
ModuleNotFoundError: No module named 'login_authentication'
ModuleNotFoundError: No module named 'login_authentication'  Hi...: No module named 'login_authentication' How to remove the ModuleNotFoundError: No module named 'login_authentication' error? Thanks   Hi
JDbc Login Authentication
JDbc Login Authentication  Pease Please.. Send Me one Login Authentication Using ComboBox.From Servlet and jsp with sessions I am new to sessions..." value="Login"><input type="reset" Value="Cancel" ></input><
Login Authentication using existing Active Directory.
Login Authentication using existing Active Directory.  I need jsp code for login authentication of username and password using already existing Active Directory. Here no need to create active directory.The JSP code should
login page php mysql example
login page php mysql example  How to create a login page for user in PHP Mysql? Please provide me a complete tutorial. Thanks in Advance
login page with mysql using jsp
login page with mysql using jsp  pls i need a sample of login page to check username and password in mysql database. thanks
Error with LogIn with mysql database
Error with LogIn with mysql database  Hi, I have followed steps from...){ System.out.println("In Check login"); Session session......Check Login"); return loginDAO.checkLogin(userName, userPassword
java code for registration and login pages, mysql as a bankend.
java code for registration and login pages, mysql as a bankend.  please send me the java code for registration and login pages and to store the data in mysql
Login Authentication using Bean and Servlet In JSP
Login Authentication using Bean and Servlet In JSP... developed a web application of login authentication using Bean  in JSP. Five...;/body> <html> Output: Login Authentication form : After
redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.
redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.  I am successfully able to create a session on successful login, but I want to display
login page using jsp servlrt with mysql database?
login page using jsp servlrt with mysql database?  Description: example:total users are 3.each use have username and password save in mysql database table login. After successfully login user1 see only index page,if user2 login
Authentication
interface for login */ } public void addLoginListener(LoginListener login) { this.login = login; } public void fireLoginEvent(LoginEvent loginE
Spring 3 MVC Login Form Example with Database MySql
Spring 3 MVC Login Form Example with Database MySql  Sir i have checked your project of Spring 3 MVC Login Form Example But Sir Not able to do It with database Mysql. Can you Provide code for login with database. Thanks
simple code to login user & authenticate it from database mysql
simple code to login user & authenticate it from database mysql  Sir, I am creating a login page which contain userid & password. Iwant to authenticate user from mysql database. please tell me the code for it. Thanks
How to view database for user when they login in netbeans and mysql?
How to view database for user when they login in netbeans and mysql?  I create a web page where when user login they have to fill in a form, after... but it come out with all the other user information too. I'm using netbeans and mysql
How to Create Login/ Registration Form using PHP and MYSQL
How to Create Login/ Registration Form using PHP and MYSQL  Hi, I am learning PHP. I have some dilemma how to create a Login Form and make connectivity with HTML page using PHP and MYSQL. Is there any body can guide me
login
login  how to login admin and user with the same fields(name & password) in single login page while the table for admin and user is seprate in database(mysql) please provide me solution
login application
login application  how to create login application ?   Hi, Please check the following tutorials: Video tutorial - JSP Login Logout Example Login Authentication using Bean and Servlet In JSP simple code to login user
login
java.awt.event.*; class Login { JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2; Login() { final JFrame f=new JFrame("Login Form..."); Connection con = DriverManager.getConnection("jdbc:mysql://localhost
login
login  how to create login page in jsp   Here is a jsp code that creates the login page and check whether the user is valid or not. 1...;tr><td></td><td><input type="submit" value="Login">
login
login  How to create login page in jsp
login
login  How to create login page in jsp
login
login  login page display an error showing failure to login even when the correct information is entered
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name
login
login  i am doing the project by using netbeens.. it is easy to use the java swing for design i can drag and drop the buttons and labels etc.. now i want the code for login.. i created design it contains the field of user name

Ads