ff

Recent Posts

Wednesday, May 8, 2013

Create simple Login useing Java (Part 2)



I guess you have completed the Part 1 of this login application tutorial, if not please go to Create simple Login application Java (part 1) and complete it.

When we develop Software, we need to store some data for future usage, we use Database Server for accomplish that objective. In this tutorial I Use MY SQL Server, but you can use Microsoft SQL Server also, if you like

Step 1
Type following queries on MySQL server command line

MYSQL > Create database dehanLogin;
MYSQL >use dehanLogin;
MYSQL > create table login(username varhcar(20) primary key, password varchar(20));

create new java class and name it as DB.java

public class DB {
    private static Connection con;
   public static Connection myConnection() throws ClassNotFoundException, SQLException{              
             Class.forName("com.mysql.jdbc.Driver");
              If(con !=null){
                                return con;
}else{
  con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbName","root","mySQLPassword");//
     return con;
}
             
    }//end myConnection()
}// end DB.java

Add mysqlconnector jar file by right clicking Library folder and select ‘add library’ then select MYSQL JDBC driver .

Step 3
Code for login button
     

            try {
          if(txtUsername.getText().equals("")){
             JOptionPane.showMessageDialog(null,"username cannot be empty");
         }else{
             String pwd=new String(txtpwd.getPassword());
             ResultSet rs =DB.myConnection().createStatement().executeQuery("select username,password from login where username = '"+txtUsername.getText()+"'and password = '"+pwd+"'").;
            if (rs.frist()){
                new Home().setVisible(true);
            }else {
            JOptionPane.showMessageDialog(null,"Incorrect username or password");
           }


         }
        }catch (NullPointerException e){
               JOptionPane.showMessageDialog(null,"password cannot be empty");
                e.printStackTrace();
        }catch (Exception e){
        
                e.printStackTrace();
        }


Try to make the login with seperate user levels
hope your comments and the problems  you get in developing time
and also you can request any java turorial by commenting on my post or just make a e-mail for me
dehanpramoda@gmail.com :)

0 comments :

Post a Comment