This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Retrieving & Comparing data from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Retrieving & Comparing data from database" Watch "Retrieving & Comparing data from database" New topic
Author

Retrieving & Comparing data from database

Shashank Anand
Greenhorn

Joined: Oct 30, 2008
Posts: 26
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

Joined: Aug 13, 2004
Posts: 1081

  • 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 ]

    Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
    Shashank Anand
    Greenhorn

    Joined: Oct 30, 2008
    Posts: 26
    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

    Joined: Aug 13, 2004
    Posts: 1081

    Yes, you can use two resultset simultaneously, but must be associated with different statement objects.



    Shailesh
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Retrieving & Comparing data from database
     
    Similar Threads
    failing to get correct response from servlet to midlet
    JDBC - Retriving data from Database
    kindly help me in redirecting....... as soon as possible
    Problem with SQL select
    apache tomcat error