Results 1 to 10 of 10
Thread: Username And Password
- 05-18-2011, 05:16 AM #1
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
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
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))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"); } } } }
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
- 05-18-2011, 05:50 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
You can store all the value of the ResultSet in an array, then loop to each array to compare to your username and password
- 05-18-2011, 06:14 AM #3
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
RichersooN can you give me an example please !
- 05-18-2011, 09:27 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
First off, when you have a succes you aren't exiting the loop:
Second, this is not how you check for things.Java Code:if (u.equals(n)&& p==i){ System.out.println("Access Granted"); // Should be exiting the loop here. }
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.
- 05-18-2011, 12:23 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
- 05-18-2011, 12:37 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Is there?
Seems fine to me.
If the usernames match and the passwords (really ids) match then access granted. (and break).Java Code:if (u.equals(n)&& p==i){ System.out.println("Access Granted"); }
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 12:40 PM.
- 05-18-2011, 12:42 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
- 05-18-2011, 12:59 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Oh yes.
So it would.
Unless they corrected their SQL...;)
- 05-18-2011, 02:02 PM #9
Member
- Join Date
- Jul 2008
- Location
- London (Kingsbury)
- Posts
- 41
- Rep Power
- 0
hey thank you very much guys
Appreciated
- 05-18-2011, 02:59 PM #10
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Similar Threads
-
New to GUI. Logging in with Password and username
By slitka in forum New To JavaReplies: 3Last Post: 04-11-2011, 11:39 PM -
password username and databases
By chalo in forum JCreatorReplies: 0Last Post: 12-02-2008, 08:11 AM -
username password verification
By bheezee in forum JDBCReplies: 0Last Post: 11-25-2008, 06:55 PM -
Help, created a username and password box
By cachi in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:21 AM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks