This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC-- Not executing rs.next()

 
Boon Subra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Bhuvana Bala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic