This is a simple log-in form written in java. You can use this form in your applications for logging in your application user. This code connects to a MySQL database(I have used the WAMP server). This code follows these steps:
1.Fetch username and password from the respective textfields.
2.Connects to the Database.
3.Fires a query which returns the password for the username entered by the user in the username textfield.
4.If the query returns no elements, it means that the username is not valid or not present in the database.
5.Checks the password. If the password matches shows login successful box else shows wrong password message box.
Code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import java.awt.*; import java.sql.*; public class MainClass{ static JTextField name,pass; static JButton submit; private static class Handler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String user = name.getText(); String strpass = pass.getText(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException ex) { Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex); } try { Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", ""); Statement st = con.createStatement(); String query = "SELECT pass FROM info where username='"+user+"'"; System.out.println(query); ResultSet rs = st.executeQuery(query); if(rs.next()) { String dbpass = rs.getString(1); if(dbpass.equals(strpass)){ JOptionPane.showMessageDialog(null,"Login Successful! ","Success",JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null,"Login Unsuccessful!","Error",1); } } else { JOptionPane.showMessageDialog(null,"Username not found","Error",1); } } catch (SQLException ex) { Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex); } } } MainClass(){ JFrame main = new JFrame("Login Form Demo- MyCoding.net"); main.setBounds(350,150,500,500); main.setVisible(true); main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); name = new JTextField(10); pass = new JTextField(10); main.setLayout(new GridLayout(0,1)); JPanel pane = new JPanel(); main.add(pane); pane.add(new JLabel("Username: ")); pane.add(name); pane.add(new JLabel("Password: ")); pane.add(pass); submit = new JButton("Submit"); pane.add(submit); submit.addActionListener(new Handler()); } public static void main(String args[]){ new MainClass(); } } |
Output:

Program tested in NetBeans IDE 7.0.
dis doesn’t wok!!!!!!!!!
Hey can you tell me what errors are you getting or what part of the code is not functioning the way it is supposed to?
Thank you very much. It works perfectly.
successfully worked for me…thanks a lot…
Thank a lot. perfectly work for me
Nice to hear that guys.
thanks…..it works perfectly
thnks a lot for this beautiful post
Compiled successfully. . .bt it didn’t run, cause i dnt knw how to create database using ms access. .can u tell me step by step? I am new in java. . . . Any help would be greatly appreciated
This program will only work with MySQL databases. If you want to use MS access then you will have to load the respective drivers.
String query = “SELECT pass FROM info where username=’”+user+”‘”;
why have u given +user+ i didn’t get that. I am working with where clause first time in jdbc.
The value of textfield ‘name’ is stored in the variable ‘user’. We have concatinated this variable with the sql query. So basically at runtime if you enter ‘tom’ in the ‘name’ textfield, the query would be
SELECT pass FROM info where username=’tom‘
The program doesn’t enter in to the If loop! My connection is correct!
check the following code:
// TODO Auto-generated method stub
String user = username.getText();
String strpass = password.getText();
try {
Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);
Statement st = con.createStatement();
String query = “SELECT password FROM login where username=’”+user+”‘”;
System.out.println(query);
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
String dbpass = rs.getString(2);
if(dbpass.equals(strpass)){
System.out.println(“inside while”);
JOptionPane.showMessageDialog(this,”correct”);
}
}
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
doesnt work!!! please help i have a project to do in one week! its not going in the if loop… db connection successful.
package com.project.jdbc;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
@SuppressWarnings(“serial”)
public class jdbc extends JFrame implements ActionListener {
JTextField username, password;
JButton login;
public jdbc()
{
setLayout(new FlowLayout());
username= new JTextField(20);
password= new JTextField(20);
login= new JButton(“Login”);
add(username);
add(password);
add(login);
login.addActionListener(this);
}
public static void main(String[] args) {
jdbc login = new jdbc();
login.setVisible(true);
login.setSize(400, 400);
login.setDefaultCloseOperation(EXIT_ON_CLOSE);
/* try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(“select * from pets”);
System.out.println(“PID \t PNAME \t PTYPE”);
while(res.next())
{
System.out.println(res.getInt(1)+”\t”+res.getString(2)+”\t”+res.getString(3));
}
res.close();
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String user = username.getText();
String strpass = password.getText();
try {
Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);
Statement st = con.createStatement();
String query = “SELECT pass FROM login where username=’”+user+”‘”;
System.out.println(query);
ResultSet rs = st.executeQuery(query);
if(rs.next())
{
String dbpass = rs.getString(“password”);
if(dbpass.equals(strpass)){
JOptionPane.showMessageDialog(null,”Login Successful!”);
}
else
{
JOptionPane.showMessageDialog(null,”Login Unsuccessful!”);
}
}
else
{
JOptionPane.showMessageDialog(null,”No Record Found”);
}
} catch (SQLException ex) {
System.out.println(e);
}
}
}
good
Nice TutoriaL
The program seems to be fine, what does this line display in the output screen.
String query = “SELECT pass FROM login where username=’”+user+”‘”;
System.out.println(query);
You can try this,
String query = “SELECT pass FROM login where username= ”+user+””;
Thank you.. Cleared a lot of my doubts..!!
This is totally working … by the way, i’m using NetBeans 7.1.1 and a WAMPSERER help by MYSQL Workbench….
the problem by me when trying to attempt to make this login form is the connection between the database and the program itself, but with the help of NetBeans it really solve the problem…
Thank You Very Much…
Create three button on the JSP which are namely Add product, edit product and delete product.
A div would be rendered on the screen on click of add product and edit product containing all the relevant fields.
On edit product, the system would render a text box where the user would be asked for the product id and then on tab of it the product details would be retrieved from the database. For delete, the on tab event would prompt for user confirmation and then on confirmation the product would be deleted from the database. You are free to opt for a better UI design than the one mentioned above
The Add product and edit product would contain the product id and legal address details like Address line1, Address line2, City, State, Post Code and Country.
On click of submit button in case of add product, edit product and edit product, appropriate user data would be maintained by the system.
Success message needs to be shown for all the 3 operations which are namely Add product, Edit product and delete product.
how to strat with m new to java
Man..you are lifesaver..:)
how to create a login webpage n link for registration if user not a already a member
You could use a session variable to check if the user has logged in or not. Depending on the value of the session variable, you can display/hide the link for registration.
Hi
Sir ,
I am in 3rd year of IT engg. I am really weak in swing components in java as compare to J2EE and any. Please tell me any way to improve it.
Please reply
nice 1