Saturday 17 October 2015

Filled Under: , ,

How To Create A Login Frame With Create New Id Option In Java

In this tutorial we are going to create a login frame using jdbc  in this login user can create a new his/her id with password.Let's start first create a new project in netbeans then create new jframe form.


FINAL RESULT:


CREATING JPANEL:

Change layout of frame by pressing right click on frame then select layout>>Flowlayout.After this pick and drop jpanel in frame from palette.Change the colour of  jpanel to make it attractive.

DESIGNING MAIN WINDOW:

Our next step is to create main window for our project for this following controls.
  1. Pick two label and drop in frame and edit text of label as shown in pictures.
  2. Select jtextfield and change it variable name to userfield.
  3.  Create jpasswordfield and change its vraible name to passfield.
  4. Then create a button and change it's variable name and text to login.
  5. Finally create a button for a register a new user option change it's variable name as register.
After creating controls design your frame as shown in below picture by changing font style and colour of label and jbutton text change layout of your frame to make it attractive.


DESIGNING DIALOG BOX :

As a user click on click new id option new dialog box should be open so we have to create a dialog box for our login frame to create dialog box  press right click on other component in navigator as shown in picture.



After this step you will see that dialog box will appear above frame in navigator  change its variable name to box press double click on it to design now pick following components from palette:
  1. Three labels and change its text.
  2. One jtextfield and change it's variable as newid.
  3. Now Create jpasswordfield and change it's variable name as newpass
  4. Create jbutton and change its variable and text as create.
Our dialog box is completed set layout and design of dialog box as shown in below picture.


To make it accessible with our main frame double click on register button and paste a below code.
  box.setVisible(true);

CREATE DATA BASE FOR LOGIN FRAME:

For this project we need a database for this frame I have created database in ms access.your next step is to create a table in ms access your table name should be logtab. Then create a data source your data source name should be login.Your table should contain two columns as shown in below picture.






CREATING METHOD FOR CREATE OPTION:

In this step we will code for create a new id option you have to simply paste a below code below constructor in code area 

public void  create(String a,String b)
{
try{
Statement st;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();
st.executeUpdate("INSERT into logtab VALUES('"+a+"','"+b+"')");
JOptionPane.showMessageDialog (null,"New ID is Succesfully Created", "Message", JOptionPane.INFORMATION_MESSAGE);

box.dispose();

}
catch(Exception e)
{
JOptionPane.showMessageDialog (null, e, "Error", JOptionPane.ERROR_MESSAGE);
}


}    

When a user clicks on create button above method should be called so double click on create button then copy below code in action performed method of create button.


String nid=newid.getText();

String pass=newpass.getText(); 
create(nid,pass);

CREATING METHOD FOR LOGIN OPTION:

In last create method for login option when user clicks on login below method should be call so paste below code after first method 
public void match(String a,String b) 
{
Boolean c = false,d=false;
try {
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
Statement st=cn.createStatement();
ResultSet data,data1;
data=st.executeQuery("select * from logtab where id='"+a+"'");
if(data.next())
{
c = true;
}
else{
System.out.print("ID Not Matched ");
}
data1=st.executeQuery("select * from logtab where password='"+b+"'");
if(data1.next())
{
d = true;
}
else
{
System.out.println("password not match");
}
if(c & d)
{
System.out.print("Successfully Login ");
catch(Exception e)
{
System.out.print(e);
}
}

after copying this code paste below code in login action performed method code 
String nid=newid.getText(); 
String pass=newpass.getText();
 create(nid,pass);

Our project is successfully created must share your feedback about this project.


1 comments:

  1. provide more example with database login and update user password !

    ReplyDelete