Results 1 to 10 of 10
  1. #1
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default Username And Password

    Hi Guys

    I am working on a project in Java and Mysql i have a table in my database to hold username and password for the application

    the table as follow

    mysql> select * from log;
    +----------+----------+
    | username | password |
    +----------+----------+
    | Razmy | 100 |
    | Raza | 200 |
    | Jhone | 300 |
    | Jay | 400 |
    | Jennifer | 500 |
    +----------+----------+
    5 rows in set (0.00 sec)

    I wrote a Java class to check the username and the password from the database. it did work well

    but got an problem

    this the class witch I wrote

    Java Code:
    
    public class ResultSET  {
    	static Connection connection;
    	static String n ="Raza";
    	static int i = 200;
    	static JPanel pl = new JPanel();
    	public static void main(String[] args) throws ClassNotFoundException, SQLException{
    		
    		Class driver = Class.forName("com.mysql.jdbc.Driver");
    		connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","asdfgh123");
    		
    		java.sql.Statement s = connection.createStatement();
    		s.executeQuery("SELECT username,password from Log");
    		ResultSet rs = s.getResultSet();
    		int count=0;
    		
    		while(rs.next()){
    		String u = rs.getString(1);
    		int p = rs.getInt(2);
    		
    		
    		if (u.equals(n)&& p==i){
    			System.out.println("Access Granted");
    		}
    		else{
    			JOptionPane.showMessageDialog(pl, "Password is Wrong");
    			
    			
    			}
    		
    		}
    		}
    
    }
    I define 2 variable to pass the username and the password to check against database ( String n --(to hold the name) ,, int i --(to hold password))

    when i run the program there is no error at all. but the problem is the java class checking the whole database. its checking each and every record in the database.

    for example if i pass the second record to the program ( witch is username = Raza password =200) and when I run the program. its checking the first record against the variable value witch i passed (first record in the table is Razmy , 100) then the program go to its else part the JOptionPane aper and then when i click ok in JOptionPane next minute its check the second record witch is rite then its print the Access Granted message then its go to the 3rd 4th 5th records and its print the password wrong message

    what i wanna do is if i pass a value throw a variable is should check the whole table and if the values are equal then it should print the access granted message. if the values are not equal it should print the password wrong message ONCE not for all records in the table.

    thank you guys for your time

  2. #2
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    You can store all the value of the ResultSet in an array, then loop to each array to compare to your username and password

  3. #3
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    RichersooN can you give me an example please !

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    First off, when you have a succes you aren't exiting the loop:
    Java Code:
    		if (u.equals(n)&& p==i){
    			System.out.println("Access Granted");
    // Should be exiting the loop here.
    		}
    Second, this is not how you check for things.
    Don't get all the data back from the database and then search through it.
    Database are designed to do the searching for you.

    SELECT username FROM Log WHERE username = ? and password = ?
    Using a PreparedStatement, and assigning the two parameters using the relevant setters in that API (setString() I suspect).

    If that returns anything in the result set (ie rs.next() returns true) then you know that is a successful login.

    There should be no need to ever get the password out of the database.

  5. #5
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    First off, when you have a succes you aren't exiting the loop:
    Java Code:
    		if (u.equals(n)&& p==i){
    			System.out.println("Access Granted");
    // Should be exiting the loop here.
    		}
    Second, this is not how you check for things.
    Don't get all the data back from the database and then search through it.
    Database are designed to do the searching for you.

    SELECT username FROM Log WHERE username = ? and password = ?
    Using a PreparedStatement, and assigning the two parameters using the relevant setters in that API (setString() I suspect).

    If that returns anything in the result set (ie rs.next() returns true) then you know that is a successful login.

    There should be no need to ever get the password out of the database.
    Yes toll is right, you can use
    Java Code:
    break;
    to exit the loop

    but there's still a bug in the invalid username condition,

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    Quote Originally Posted by RichersooN View Post
    but there's still a bug in the invalid username condition,
    Is there?
    Seems fine to me.
    Java Code:
    if (u.equals(n)&& p==i){
        System.out.println("Access Granted");
    }
    If the usernames match and the passwords (really ids) match then access granted. (and break).

    Though this is unecessary if the OP writes the query correctly.

    ETA: Or is it the Swing code that's wrong?
    Last edited by Tolls; 05-18-2011 at 01:40 PM.

  7. #7
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Is there?
    Seems fine to me.
    Java Code:
    if (u.equals(n)&& p==i){
        System.out.println("Access Granted");
    }
    If the usernames match and the passwords (really ids) match then access granted. (and break).

    Though this is unecessary if the OP writes the query correctly.

    ETA: Or is it the Swing code that's wrong?
    Yes if the current resultset is not equal to the valid username theirs a prompt, so there would be a multiple prompt

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    Oh yes.
    So it would.

    Unless they corrected their SQL...;)

  9. #9
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    hey thank you very much guys
    Appreciated

  10. #10
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

Similar Threads

  1. New to GUI. Logging in with Password and username
    By slitka in forum New To Java
    Replies: 3
    Last Post: 04-12-2011, 12:39 AM
  2. password username and databases
    By chalo in forum JCreator
    Replies: 0
    Last Post: 12-02-2008, 09:11 AM
  3. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 07:55 PM
  4. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 12:41 AM

Posting Permissions

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