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.shtmlThanks
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);
}
}
Related Tutorials/Questions & Answers:
Login authentication & mysql - Java BeginnersLogin authentication & mysql Hi ,
Could you guide or provide sample coding for the following scenerio.
I need to code
authentication for user...
Authentication with
MySql using Java Swing:
1)Create a java class
Login authenticationLogin authentication i want d code for
login authentication from
mysql database on the same pc using swings
Advertisements
Login authenticationLogin authentication i want d code for
login authentication from
mysql database on the same pc using swings
login authentication - Java Beginnerslogin authentication i 've designed the jsp page for
login. now i need to connect it with the bean and do
authentication for the
login for the "register" table in
mysql. can anybody help me with the code immediately
login authentication in jsp and servletlogin 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 AuthenticationStruts
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
JDbc Login AuthenticationJDbc
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 examplelogin page php
mysql example How to create a
login page for user in PHP
Mysql? Please provide me a complete tutorial.
Thanks in Advance
Error with LogIn with mysql databaseError 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
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 PHP MySQL Login FormLogin Form using PHP and
MySQL:
In any website generally it is mandatory to have a
login form to keep your
data secure. In this regard we need to have... will study how to create a
login form,
connect with the database server and
login Spring 3 MVC Login Form Example with Database MySqlSpring 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
loginlogin 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 applicationlogin 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
loginlogin 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">
loginlogin How to create
login page in jsp
loginlogin How to create
login page in jsp
loginlogin
login page display an error showing failure to
login even when the correct information is entered
Login Authentication in JSP from the
database using
MySQL and we are forwarding this servlet data...;
For getting values from database we are using
MySQL database...; "jdbc:
mysql://192.168.10.59/messagepaging";
 
loginlogin i want to now how i can write code for form
login incolude user and password in Jcreator 4.50
Hello Friend,
Visit HereADS_TO_REPLACE_1
Thanks
loginlogin 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
loginlogin 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
loginlogin 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
loginlogin 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
Authentication interface for
login */
}
public void addLoginListener(LoginListener
login)
{
this.login =
login;
}
public void fireLoginEvent(LoginEvent loginE
Login ProgramLogin Program
Login program in struts to set username & password in session. and this session is available in all screens for
authentication...
Hi,
Please read it at:ADS_TO_REPLACE_2
Login/Logout With Session
login pagelogin page pls say how to create a
login page in jsp and
mysql using netbaens
Hi Friend,
Please visit the following links:ADS_TO_REPLACE_1
http://roseindia.net/jsf/netbeans/index.shtml
http://roseindia.net/jsp
Login Form of +,on clicking which a
login form appears.
Only the middle column is different for each of the page.
How to write a code for
login authentication so...
Login Form I have 8 jsp pages.Each of them has three columns:Left
Login Form of +,on clicking which a
login form appears.
Only the middle column is different for each of the page.
How to write a code for
login authentication so...
Login Form I have 8 jsp pages.Each of them has three columns:Left
Login Screen of Application
Login Screen of Application
Login Screen our web application allows the user... in the database and
once the
authentication is successful user is allowed
MySqlMySql what is default password of
mySql, and how i configure
mySql.
Hi,
If you are installing
MySQL on windows then you will have...
You may try to
login as:
User: root
Password: Blank (not password) just press