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))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

