ResultSet Problem
Hi friends,
I am a novice in Java. I am facing some problem in retrieving values from a database. The following is the code snippet:
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbapp","root","*********");
PreparedStatement pstmt0 = con.prepareStatement("SELECT PassWord FROM user_authentication Where UserName = ?");
pstmt0.setString(1,uN);
System.out.println("Working!");
ResultSet rs = pstmt0.executeQuery();
while (rs.next()){
pw = rs.getString("PassWord");
System.out.println(pw);
}
The database is named sa 'dbapp' and the table is 'user_authentication'. There are three columns in the table out of which the value in the column named 'UserName' is used as the primary-key. The other two columns are named as 'PassWord' and 'UserRole' respectively.
An entry having UserName as xyz is stored in the table.
When i check by manually typing the following query (in the SQL-user-interface)"SELECT PassWord FROM user_authentication Where UserName = 'xyz';", i get the corresponding password for that particular row.
But when i run the above program i get an empty result-set. Why is it so? Could anybody help me out?
>pstmt0.setString(1,uN);
what is the value of uN?
> con=DriverManager.getConnection("jdbc:mysql://localhos
> t:3306/dbapp","root","*********");
You know you should not use swear words. ;)
> But when i run the above program i get an empty
> result-set. Why is it so? Could anybody help me out?
It means there is no entry for the UserName you set to the query, hence no PassWord and no ResultSet.
The above code is part of an Action class. I am inputting the user(xyz) name through a jsp page. Everything above this part is working fine. But still i am getting an empty result set. Kindly help.
> >
> con=DriverManager.getConnection("jdbc:mysql://localhos
>
> > t:3306/dbapp","root","*********");
>
> You know you should not use swear words. ;)
>
> > But when i run the above program i get an empty
> > result-set. Why is it so? Could anybody help me
> out?
>
> It means there is no entry for the UserName you set
> to the query, hence no PassWord and no ResultSet.
Aaahhh, but there is a ResultSet, it's just empty. ;-)
> The above code is part of an Action class. I am
> inputting the user(xyz) name through a jsp page.
> Everything above this part is working fine. But still
> i am getting an empty result set. Kindly help.
Try printing out what user name you are sending to the query (check for trailing spaces...). Try executing the same query through a query tool and see if you are getting a result.
> > to the query, hence no PassWord and no
> ResultSet.
>
> Aaahhh, but there is a ResultSet, it's just empty.
> ;-)
You have been waiting to nitpick on me, haven't you? ;-)
Yes, you are right! There is a ResultSet that contains nothing.
> The above code is part of an Action class. I am
> inputting the user(xyz) name through a jsp page.
> Everything above this part is working fine. But still
> i am getting an empty result set. Kindly help.
I would say, first of all, to print out the variable uN somewhere, and make sure that it contains what you expect it to contain. But, as long as there are no exceptions being thrown, it is simply not finding the provided username.
Edit: And I see I was too late on this, as well.
> > > to the query, hence no PassWord and no
> > ResultSet.
> >
> > Aaahhh, but there is a ResultSet, it's just empty.
> > ;-)
>
> You have been waiting to nitpick on me, haven't you?
> ;-)
>
> Yes, you are right! There is a ResultSet that
> contains nothing.
It's a dirty job, but somebody's got do it! ;-)
> It's a dirty job, but somebody's got do it! ;-)
It's still better than cleaning elephants in a zoo.
> > It's a dirty job, but somebody's got do it! ;-)
>
> It's still better than cleaning elephants in a zoo.
Especially the ones that have a tendancy to back up and make your head disappear. ;-)
You're correct!
Though i am typing in the UserName in the JSP page only a null value is being passed.
What could be the problem? Kindly help.
> You're correct!
>
> Though i am typing in the UserName in the JSP page
> only a null value is being passed.
>
> What could be the problem? Kindly help.
null value is not being passed. Your variable has null because it is not getting what you expected. I can precisely comment on the cause of the problem but I suspect you are using request.getParameter() to get the field value, so you could start by printing out the value from the getParameter(). Also, check if the field name is specified correctly.
Check the name of the password variable.
Okay, i got my error.
I did not cast the form into an ActionForm. But there are still more errors.
> Okay, i got my error.
>
> I did not cast the form into an ActionForm. But there
> are still more errors.
You mean there were errors all this time?
I have solved the problems and the program is working fine.
The UserName not being passed was true and it was due what i had stated earlier but some more small errors were there but those have been solved.
Thanks for your help.