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?

[1295 byte] By [subhashmedhia] at [2007-11-27 10:28:52]
# 1

>pstmt0.setString(1,uN);

what is the value of uN?

Yannixa at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 2

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

aniseeda at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

subhashmedhia at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> >

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

masijade.a at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 5

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

aniseeda at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 6

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

aniseeda at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 7

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

masijade.a at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 8

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

masijade.a at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 9

> It's a dirty job, but somebody's got do it! ;-)

It's still better than cleaning elephants in a zoo.

aniseeda at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 10

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

masijade.a at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 11

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.

subhashmedhia at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 12

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

aniseeda at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 13

Check the name of the password variable.

BIJ001a at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 14

Okay, i got my error.

I did not cast the form into an ActionForm. But there are still more errors.

subhashmedhia at 2007-7-28 17:54:12 > top of Java-index,Java Essentials,Java Programming...
# 15

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

aniseeda at 2007-7-28 17:54:17 > top of Java-index,Java Essentials,Java Programming...
# 16

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.

subhashmedhia at 2007-7-28 17:54:17 > top of Java-index,Java Essentials,Java Programming...