Results 1 to 4 of 4
  1. #1
    syferite is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Login validation problems!

    I have an issue with my program, whereby when I type a username and password (that has been registered in a database), it says that "Username doesn't exist", when it should be existing!

    If possible, could anyone be willing to look through my codes and see where I went wrong?

    UserTable.java:
    Java Code:
    package olympians.entity;
    
    
    
    import java.sql.ResultSet;
    import olympians.database.DBController;
    
    public class UserTable {
    	
    	private static final String String = null;
    	private static final String LoginID = null;
    	private String ID;
    	private String password;
    	private String name;
    	private String nric;
    	private String gender;
    	private String dob;
    	private String address;
    	private String email;
    	private String mobilenumber;
    	
    	public UserTable () { }
    	
    	public UserTable(String ID, String password)
    	{
    		this.ID = ID;
    		this.password = password;
    	}
    	
    	
    	public String getID() {
    		return ID;
    	}
    	public void setID(String id) {
    		ID = id;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getNric() {
    		return nric;
    	}
    	public void setNric(String nric) {
    		this.nric = nric;
    	}
    	public String getGender() {
    		return gender;
    	}
    	public void setGender(String gender) {
    		this.gender = gender;
    	}
    	public String getDob() {
    		return dob;
    	}
    	public void setDob(String dob) {
    		this.dob = dob;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	public String getMobilenumber() {
    		return mobilenumber;
    	}
    	public void setMobilenumber(String mobilenumber) {
    		this.mobilenumber = mobilenumber;
    	}
    	
    	
    	public boolean createUser(){
    		boolean success = false;
    		DBController db = new DBController();
    		db.setUp("Olympians");
    		String sql = "INSERT INTO User(UserID, UserPassword, UserName, UserNRIC, UserGender, UserDOB, UserAddress, UserEmail, UserMobileNumber) ";
    		sql += "VALUES ('" + ID + "', '" + password + "', '" + name + "', '" + nric + "', '" + gender + "', '" + dob + "', '" + address + "', '"+ email + "','" + mobilenumber + "')";
    		if (db.updateRequest(sql)==1)
    			success = true;
    		db.terminate();
    		return success;
    	}
    	
    	public boolean retrieveUser(String name){
    		boolean success = false;
            ResultSet rs = null;
    		DBController db = new DBController();
            db.setUp("Olympians");
            String dbQuery = "SELECT * FROM User WHERE UserName ='" + name +"'";
    		
            rs = db.readRequest(dbQuery);
            try{
            if (rs.next()){
    
    			ID = rs.getString("UserID");
       	        password = rs.getString("UserPassword");
       	        name = rs.getString("UserName");
       	        
       	       System.out.println(name);
       	    System.out.println(name); System.out.println(name); System.out.println(name);
       	          success = true;     
       	          }
            }
    		catch (Exception e) {
    		   e.printStackTrace();
            }
     
    		db.terminate();
    		return success;
    		}
    
    		 		
         
        public boolean updateUser(){
    	 
    
    	boolean success = false;
    	DBController db = new DBController();
        db.setUp("Olympians");
    	String dbQuery = "UPDATE USERSET ( UserPassword, UserName, UserNRIC, UserGender, UserDOB, UserAddress, UserEmail, UserMobileNumber)";
            dbQuery = dbQuery + "VALUES ('" + password + "', '"+ name + "','" + nric + "','" + gender
    		+ "','" + dob + "','" + address + "','" + email + "','" + mobilenumber + "')";
        
    	if (db.updateRequest(dbQuery) == 1){
        success = true;
    	}
    
    	db.terminate();
    	return success;
    	}
    
    
        public boolean deleteUser(){
    	 
    	boolean success = false;
    	DBController db = new DBController();
        db.setUp("Olympians");
    	String dbQuery = "DELETE FROM UserID = ( UserPassword, UserName, UserNRIC, UserGender, UserDOB, UserAddress, UserEmail, UserMobileNumber)";
            dbQuery = dbQuery + "VALUES ('" + password + "', '"+ name + "','" + nric + "','" + gender
    		+ "','" + dob + "','" + address + "','" + email + "','" + mobilenumber + "')";
    	if (db.updateRequest(dbQuery) == 1){
    
    	        success = true;
    	}
    
    	db.terminate();
    	return success;
    	 
    	 
    	 }
    
        public boolean isExist(){
    		boolean result=false;
    		ResultSet rs = null;
    		String dbQuery = "SELECT * FROM UserTable WHERE id =\'" +
    				ID + "\' and password =\'" + password  + "\'";
    		DBController db = new DBController();
    		System.out.println(dbQuery);
    		db.setUp("Olympians");
    		rs = db.readRequest(dbQuery);
    		try{
    			if (rs.next())
    				result = true;
    			else
    				result = false;
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    		db.terminate();
    
    		//if (ID.equals("testing")&& password.equals("test"))
    		//	result = true;
    		//else 
    			//result = false;
    		
    		return result;
    	}
     
        public boolean isCheckUserExist2(String inID){
    		boolean result=false;
    		ResultSet rs = null;
    		String dbQuery = "SELECT * FROM User WHERE UserID  ='" +
    				inID + "'" ;
    		DBController db = new DBController();
    		db.setUp("Olympians");
    		rs = db.readRequest(dbQuery);
    		try{
    			if (rs.next())
    				result = true;
    			else
    				result = false;
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    		db.terminate();
    		return result;
    	}
        
        public static void main(String[] args){
         //	UserTable u = new UserTable();
        	//u.setAddress("address");
        	//u.setDob("12122000");
        	//u.setEmail("a@b.c");
        	//u.setGender("F");
        	//u.setMobilenumber(987654321);
        	//u.setName("my name");
        	//u.setNric("s7654321A");
        	//u.setPassword("12324");
        	//u.createUser();
        }
    
    }
    Login.java:
    Java Code:
    package olympians.ui;
    
    import javax.swing.JPanel;
    import java.awt.Dimension;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import java.awt.Point;
    import javax.swing.JOptionPane;
    import olympians.entity.UserTable;
    import javax.swing.JPasswordField;
    
    public class Login extends JPanel {
    	private static final long serialVersionUID = 1L;
    	private static final String ID = null;
    	private JLabel jLabelUserLogin = null;
    	private JLabel jLabelImage = null;
    	private JLabel jLabelImage2 = null;
    	private JLabel jLabelUserName = null;
    	private JTextField jTextFieldUserName = null;
    	private JLabel jLabelPassword = null;
    	private JButton jButtonSubmit = null;
    	private MainFrame myFrame = null;
    	private int catID;
    	private JPasswordField jPasswordFieldPwd = null;
    
    	/**
    	 * This is the default constructor
    	 */
    	public Login(MainFrame f, int CatID) {
    		super();
    		myFrame = f;
    		this.catID = catID;
    		initialize();
    
    	}
    
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize() {
    		jLabelPassword = new JLabel();
    		jLabelPassword.setBounds(new Rectangle(41, 271, 152, 25));
    		jLabelPassword.setFont(new Font("Century Gothic", Font.BOLD, 18));
    		jLabelPassword.setText("Password");
    		jLabelUserName = new JLabel();
    		jLabelUserName.setBounds(new Rectangle(43, 224, 147, 28));
    		jLabelUserName.setFont(new Font("Century Gothic", Font.BOLD, 18));
    		jLabelUserName.setText("User Name : ");
    		jLabelImage2 = new JLabel();
    		jLabelImage2.setBounds(new Rectangle(637, 16, 200, 244));
    		jLabelImage2.setIcon(new ImageIcon(getClass().getResource(
    				"/olympians/image/2810329848_79bdc685b6_m.jpg")));
    		jLabelImage2.setText("JLabel");
    		jLabelImage = new JLabel();
    		jLabelImage.setBounds(new Rectangle(204, 61, 340, 128));
    		jLabelImage.setIcon(new ImageIcon(getClass().getResource(
    				"/olympians/image/Picture1.png")));
    		jLabelImage.setText("");
    		jLabelUserLogin = new JLabel();
    		jLabelUserLogin.setBounds(new Rectangle(328, 23, 98, 28));
    		jLabelUserLogin.setFont(new Font("Century Gothic", Font.BOLD, 18));
    		jLabelUserLogin.setText("User Login ");
    		this.setLayout(null);
    		this.setLocation(new Point(0, 0));
    		this.setSize(new Dimension(830, 640));
    		this.add(jLabelUserLogin, null);
    		this.add(jLabelImage, null);
    		this.add(jLabelImage2, null);
    		this.add(jLabelUserName, null);
    		this.add(getJTextFieldUserName(), null);
    		this.add(jLabelPassword, null);
    		this.add(getJButtonSubmit(), null);
    		this.add(getJPasswordFieldPwd(), null);
    	}
    
    	/**
    	 * This method initializes jTextFieldUserName
    	 * 
    	 * @return javax.swing.JTextField
    	 */
    	private JTextField getJTextFieldUserName() {
    		if (jTextFieldUserName == null) {
    			jTextFieldUserName = new JTextField();
    			jTextFieldUserName.setBounds(new Rectangle(209, 225, 223, 27));
    		}
    		return jTextFieldUserName;
    	}
    
    	/**
    	 * This method initializes jButtonSubmit
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getJButtonSubmit() {
    		if (jButtonSubmit == null) {
    			jButtonSubmit = new JButton();
    			jButtonSubmit.setBounds(new Rectangle(212, 314, 109, 25));
    			jButtonSubmit.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
    			jButtonSubmit.setText("Submit");
    			jButtonSubmit.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					
    					String ID = getJTextFieldUserName().getText();
    					String idcheck = "";
    					String Password = new String(getJPasswordFieldPwd().getPassword());
    				
    					UserTable a = new UserTable();
    					a.retrieveUser(ID);
    					
    					idcheck = a.getName();
    					System.out.println(idcheck);
    					if (ID.equals(idcheck))
    					{
    						JOptionPane.showMessageDialog(null, "Please enter password");				
    					}
    					else
    					{
    						JOptionPane.showMessageDialog(null, "Username doesn't exist");				
    					}
    					
    					
    					
    					
    					//if (ID.equals("") || password.equals(""))
    						//JOptionPane.showMessageDialog(null, "Please enter both text field");					
    					//else{
    						 //if (userTable.isExist())
    						//JOptionPane.showMessageDialog(null, "Successful Login");
    						// else{
    							// JOptionPane.showMessageDialog(null, "Invalid Login. Try again");
    						// }
    							 
    						 
    							//myFrame.getContentPane().removeAll(); 
    							//myFrame.getContentPane().add(panel); 
    							//myFrame.getContentPane().validate(); 
    							//myFrame.getContentPane().repaint();					
    							getJTextFieldUserName().setText("");
    							getJPasswordFieldPwd().setText("");
    							
    						//	getJTextFieldPassword
    						//	getJTextFieldUserName
    					//}
    				}
    			});
    		}
    		return jButtonSubmit;
    	}
    
    	//JPanel panel = new HomePage(myFrame,5); 
    	
    	private JPasswordField getJPasswordFieldPwd() {
    		if (jPasswordFieldPwd == null) {
    			jPasswordFieldPwd = new JPasswordField();
    			jPasswordFieldPwd.setBounds(new Rectangle(214, 270, 217, 27));
    			JPanel panel = new HomePage(myFrame,5); 
    			myFrame.getContentPane().removeAll(); 
    			myFrame.getContentPane().add(panel); 
    			myFrame.getContentPane().validate(); 
    			myFrame.getContentPane().repaint();	
    			
    		}
    		return jPasswordFieldPwd;
    		
    	}
    	
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    1.You have to clean up myour and format it properly.
    2.Put comments on top of each method
    3.Before comparing the string trim it like this below in order to avoid spaces
    4.Use PreparedStatement instead of Statement.

    ex :if (ID.trim().equals(idcheck.trim()))
    {
    JOptionPane.showMessageDialog(null, "Please enter password");
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Username doesn't exist");
    }
    Ramya:cool:

  3. #3
    Epsan is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    hi everybody me Epsan new to this forum and i have a problem in database validation my
    code is like this....



    // User Bean
    package ritt;

    import java.sql.*;
    import java.util.*;


    public class UserBean
    {
    private String lname;
    private String pwd;

    public void setLname(String lname)
    { this.lname = lname; }

    public String getLname()
    { return lname; }

    public void setPwd(String pwd)
    { this.pwd= pwd; }

    public String getPwd()
    { return pwd; }

    public boolean login()
    {
    boolean logged = false;
    Connection con = null;
    PreparedStatement ps = null;
    try
    {
    con = DBUtil.getConnection();
    ps = con.prepareStatement("select lname from users where lname = ? and pwd= ?");
    ps.setString(1,lname);
    ps.setString(2,pwd);
    ResultSet rs = ps.executeQuery();
    while(rs.next())
    {
    if(lname.equals(rs.getString(1)) && pwd.equals(rs.getString(2)))
    {
    logged=true;
    }
    }
    }

    catch(Exception ex)
    {
    System.out.println( ex.getMessage());
    }
    finally
    {
    clean(con,ps);
    }
    return logged;
    } // end of login

    public void clean(Connection con, PreparedStatement ps)
    {
    try
    {
    if ( ps != null ) ps.close();
    if ( con != null) con.close();
    }
    catch(Exception ex)
    { System.out.println(ex.getMessage()); }
    }

    public boolean updatePassword(String newpwd)
    {
    Connection con = null;
    PreparedStatement ps= null;

    try
    {
    con = DBUtil.getConnection();
    String cmd = "update users set pwd=? where lname = ? ";

    ps = con.prepareStatement(cmd);
    ps.setString(1,newpwd);
    ps.setString(2,lname);

    int cnt = ps.executeUpdate();
    if ( cnt==1 )
    {
    pwd = newpwd;
    return true;
    }
    else
    return false;

    }
    catch(Exception ex)
    {
    System.out.println( ex.getMessage());
    return false;
    }
    finally
    {
    clean(con,ps);
    }


    } // end of updatePassword

    } // end of bean


    database
    users table

    lname pwd
    admin a

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

Similar Threads

  1. VAlidation
    By chetna1982 in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 07:08 AM
  2. login.jsp
    By roo7 in forum Enterprise JavaBeans (EJB)
    Replies: 5
    Last Post: 12-02-2008, 03:58 PM
  3. Replies: 0
    Last Post: 06-25-2008, 12:41 PM
  4. problems with my JSP login page
    By ilangocal in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-28-2008, 06:40 PM
  5. problems with Login
    By Peter in forum Java Servlet
    Replies: 2
    Last Post: 06-12-2007, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •