aspose file tools*
The moose likes JDBC and the fly likes JDBC-- Not executing rs.next() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC-- Not executing rs.next()" Watch "JDBC-- Not executing rs.next()" New topic
Author

JDBC-- Not executing rs.next()

Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
Hi all,
I am working on SQL Server 2000 DataBase ,Tomcat 3.2.2,JSP in Visual Age for Java.When I try to put the data into a hashtable after fetching from the database it returns null in the hashtable.Pl. help.I am tottally stuck up.

public java.util.Hashtable getAccessType(String Username) {

java.sql.PreparedStatement pstmt8 = null;
if(conn == null)
{
return null;
}
try
{
java.util.Hashtable ht1= new java.util.Hashtable();
pstmt8 = conn.prepareStatement("select [lawportal].[dbo].[acuserlogin].[AC_userid],[lawportal].[dbo].[acuserlogin].[AC_userlogin],[lawportal].[dbo].[acuserlogin].[AC_password],[lawportal].[dbo].[acuserlogin].[AC_accesst ype] from [lawportal].[dbo].[acuserlogin] where
[lawportal].[dbo].[acuserlogin].[AC_userlogin]= \'" +Username + "\'");
java.sql.ResultSet RS = pstmt8.executeQuery();
RS.next();
if(RS.next())
{

System.out.println(RS.getInt(1))// I get the output.
ht1.put("AC_userid", new Integer(RS.getInt(1)).toString());
ht1.put("AC_userlogin",RS.getString(2));
ht1.put("AC_password",RS.getString(3));
ht1.put("AC_accesstype",RS.getString(4));

}
RS.close();
pstmt8.close();
return ht1;
}
catch(Exception e ){
System.out.println("Error" + e);

try
{

if(pstmt8!=null)
{
pstmt8.close();
}
}
catch (Exception e1){}
}
return null;
}
I am able to print the value of rs.getInt(1) in the loop.But still the hashtable returns a null.
Bye..
Shiva Kumar
Ranch Hand

Joined: Feb 09, 2001
Posts: 30
Hi

1. java.sql.ResultSet RS = pstmt8.executeQuery();
2. RS.next();
3. if(RS.next())
4. {
5. ***System.out.println(RS.getInt(1))// I get the output.
6. ***ht1.put("AC_userid", new Integer(RS.getInt(1)).toString());
7. ht1.put("AC_userlogin",RS.getString(2));
8. ht1.put("AC_password",RS.getString(3));
9. ht1.put("AC_accesstype",RS.getString(4));
10. }
11. RS.close();
After Getting First String from ResultSet, not possible to get the Same once again. You did that in Line No.5 and 6. Just Remove Line No.5 and Execute. It will work.
Another thing in Line 2. you just put RS.next() suppose if your Query return more than One Row you will get the Loaded Hashtable
other wise also you will get null.
Shiva.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"Bhuvana B",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bhuvana Bala
Greenhorn

Joined: Jul 19, 2001
Posts: 10
Hi shiva,
Thanks a lot .It worked
Regards,Bhuvana
Originally posted by Shiva Kumar:
Hi

1. java.sql.ResultSet RS = pstmt8.executeQuery();
2. RS.next();
3. if(RS.next())
4. {
5. ***System.out.println(RS.getInt(1))// I get the output.
6. ***ht1.put("AC_userid", new Integer(RS.getInt(1)).toString());
7. ht1.put("AC_userlogin",RS.getString(2));
8. ht1.put("AC_password",RS.getString(3));
9. ht1.put("AC_accesstype",RS.getString(4));
10. }
11. RS.close();
After Getting First String from ResultSet, not possible to get the Same once again. You did that in Line No.5 and 6. Just Remove Line No.5 and Execute. It will work.
Another thing in Line 2. you just put RS.next() suppose if your Query return more than One Row you will get the Loaded Hashtable
other wise also you will get null.
Shiva.

A Agrawal
Ranch Hand

Joined: Jul 13, 2001
Posts: 41
By the way, which JDBC are you using ? if you use JDBC 2.0+ (3.0 is also available), You won't face this problem.
------------------
Amit Agrawal,
New Delhi, India.


Amit Agrawal,<BR>New Delhi, India.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC-- Not executing rs.next()