Java WAP to create an attractive log in frame with user name and password. Display proper message whether log in successful or not.
/* Create login table*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.sql.*;
import java.io.File;
import java.io.*;
public class dbprg6 extends Frame implements ActionListener
{
Font f;
Label lbltitle,lbluuname,lblupass,lblurepass,lbllogin;
TextField txtuname,txtpassword,txtrepassword;
Button btnlogin,btncancel,btncreate;
File db;
String path,url,sql,query,uname,upass,urepass;
Connection con;
Statement st;
ResultSet rs;
int nvg=1;
int ret;
boolean flag=true;
public dbprg6(String title)
{
super(title);
setLayout(null);
setBackground(Color.pink);
f=new Font("Palatino Linotype",Font.PLAIN,20);
setBackground(Color.pink);
setFont(f);
lbltitle=new Label("Log In");
lbluuname=new Label("User Name :");
lblupass=new Label("Password :");
lblurepass=new Label("RePassword :");
lbllogin=new Label("");
txtuname=new TextField();
txtpassword=new TextField();
txtrepassword=new TextField();
btnlogin=new Button("Log In");
btncancel=new Button("Cancel");
btncreate=new Button("Create User");
btnlogin.addActionListener(this);
btncancel.addActionListener(this);
btncreate.addActionListener(this);
add(lbltitle);
add(lbluuname);
add(lblupass);
add(lblurepass);
add(lbllogin);
add(txtuname);
add(txtpassword);
add(txtrepassword);
add(btnlogin);
add(btncancel);
add(btncreate);
lbltitle.setFont(f);
lbltitle.setBounds(225,40,130,30);
lbluuname.setBounds(40,90,120,25);
lblupass.setBounds(40,140,120,25);
lblurepass.setBounds(20,190,140,25);
lbllogin.setBounds(40,270,300,25);
txtuname.setBounds(160,90,250,25);
txtpassword.setBounds(160,140,250,25);
txtrepassword.setBounds(160,190,250,25);
btncreate.setBounds(20,230,125,30);
btnlogin.setBounds(170,230,80,30);
btncancel.setBounds(260,230,80,30);
txtpassword.setEchoChar('*');
txtrepassword.setEchoChar('*');
lblurepass.setVisible(false);
txtrepassword.setVisible(false);
addWindowListener(new WindowAdapter()
{
public void windowChanged(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae)
{
try
{
if(ae.getSource()==btncancel)
{
clear1();
}
if(ae.getSource()==btnlogin)
{
login();
}
if(ae.getSource()==btncreate)
{
add1();
}
}
catch(Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public static void main(String args[])
{
dbprg6 dp3=new dbprg6("Sanjay Chauhan");
dp3.setSize(500,350);
dp3.setVisible(true);
}
public void add1()
{
try
{
if(flag==true)
{
lblurepass.setVisible(true);
txtrepassword.setVisible(true);
flag=false;
}
else
{
connect();
st=con.createStatement();
uname=txtuname.getText();
upass=txtpassword.getText();
urepass=txtrepassword.getText();
if(uname.length()>0 && upass.length()>0 && urepass.length()>0)
{
if(upass.equals(urepass))
{
sql="insert into login values('"+uname+"','"+upass+"','"+urepass+"');";
ret=st.executeUpdate(sql);
if(ret==0)
{
System.out.println("Record Not Inserted........");
}
else
{
lbllogin.setText("User Created....!!!");
System.out.println("Record Inserted Successfully");
}
}
}
clear1();
flag=true;
lblurepass.setVisible(false);
txtrepassword.setVisible(false);
close1();
}
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
db=new File("Assign3.accdb");
path=db.getAbsolutePath();
url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+path+";";
con = DriverManager.getConnection(url, "username", "password");
System.out.println("Connection Succesfull");
st=con.createStatement();
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void close1()
{
try
{
st.close();
con.close();
}
catch(Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void login()
{
try
{
String nam,pass;
boolean x=true;
nam=txtuname.getText();
pass=txtpassword.getText();
connect();
query="select * from login";
rs=st.executeQuery(query);
while(rs.next())
{
uname=rs.getString("user");
upass=rs.getString("password");
if(nam.equals(uname) && pass.equals(upass))
{
lbllogin.setText("Log In SuccessFul.........!!!");
x=false;
break;
}
}
if(x==true)
{
lbllogin.setText("Invalid Usename Or Password");
}
close1();
}
catch(Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void clear1()
{
txtuname.setText("");
txtpassword.setText("");
txtrepassword.setText("");
}
}
Login with database in Java
Reviewed by Unknown
on
8:55 pm
Rating:
No comments: