Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving & Comparing data from database

 
Shashank Anand
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is Shashank here.

I have created a user_table with 2 fields: user_name and pass_word.
I want to design a GUI that has a textfield (text) and password field (pass), that allows the user to enter name and password and checks them with the user_table for correctness. I wrote the following code:
// actionPerformed method
try {
String name= text.getText();
char [] pw= pass.getPassword();
String str= new String (pw);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbcdbc:MyDSN", "MyLogin", "MyPassword");
Statement stmt=con.createStatement ();
ResultSet rs= stmt.executeQuery ("SELECT user_name, pass_word FROM user_table");
while (rs.next())
{
if ((name==rs.getString(1)) && (str==rs.getString(2))) {//..allow login..}
else {JOptionPane.showMessageDialog(this, "Invalid UserID /Password", "Message Dialog", JOptionPane.INFORMATION_MESSAGE); text.setText(""); pass.setText ("");}
}
con.close();
}
catch (Exception ex) { System.out.println ("Error:" + ex); }

The above code won't work. Can anybody tell me how to go about this?

Regards,
Shashank.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • You do not have any where clause in your query

  • Use equals or equalsIgnoreCase while comparing string




  • Shailesh
    [ November 14, 2008: Message edited by: Shailesh Chandra ]
     
    Shashank Anand
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Shailesh. Now my code is working fine. By the way I have another query : can we use two ResultSets in a program simultaneously, like,
    ResultSet rs, rs1;

    Regards,
    Shashank.
     
    Shailesh Chandra
    Ranch Hand
    Posts: 1082
    Java Oracle Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you can use two resultset simultaneously, but must be associated with different statement objects.



    Shailesh
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic