Solved

can I use rs.getString("name") directly instead of strName?

Posted on 2001-08-23
7
333 Views
Last Modified: 2012-05-04
hi everybody,
    I met a problem when I want to access database using Java,the code like the following:
    .......
     public void display(Connection con)
    {
     try{
      String strSQL="select name, age from worker";
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery(strSQL);
     if(rs.getString("name").trim().equals("tom"))
     {....
     }
    }catch(){}
   }

   the problem is that it will throw nullpointexception if the field name's value  is null.
 if I change the code like this,even the name's value is null, it will not throw nullpointexception:
    .....    
   public void display(Connection con)
    {
     String strName="";
     try{
      String strSQL="select name, age from worker";
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery(strSQL);
      while(rs.next())
      {
         strName=rs.getString("name");
      }
     if(strName.trim().equals("tom"))
     {....
     }
    }catch(){}
   }

 
 anybody know how to do it if I want do the first situation and I don't hope it throws the nullpointexception even the field's value is null?
  thanks.  
0
Comment
Question by:lisayan
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6419877
Hi!

You definitely need to do a rset.next(). This call allows to browse through the records in a resultset.

In the later case, the rs.next() is returning a false value because the query has not selected any record. So, the 'strName' is not set again and it retains the previous value.

The better way to handle this situation is:

while(rs.next())
     {
        strName=rs.getString("name");
if((strName != null) && strName.trim().equals("tom"))
    {....
    }
   
     }

Hope it helps!


   
0
 
LVL 6

Expert Comment

by:kotan
ID: 6420482
If you make sure that you only need to retrive the first record,

try this,

rs.next(); // should include this.
strName = rs.getString("name") == null ? "" : rs.getString("name");
if (strName.trim().equals("tom"))
{
   ...
}
0
 

Author Comment

by:lisayan
ID: 6422124
thanks,everybody. what I try to do is to use rs.getString("name") directly instead of using strName. because there are many fields in a resultset, I don't want to declare them and I only want to use them when I need them. the code like the following:
  ...
  while(rs.next())
  {
   ....
   if(rs.getString("name").equals("tom")){
      ...
   }
 .....
 instead of like this:
  ...
  while(rs.next())
  {
   ...
   strName=rs.getString("name");//declare it
   ...
   if(strName.equals("tom")){
      ....
   }
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:sghosh092199
ID: 6422178
That will be fine. In case you are using strName, it will assume different values for each record.

0
 

Author Comment

by:lisayan
ID: 6422347
the problem is that it will throw nullpointexception if name's value is null. if I use strName, even the name's value is null, it will not throw nullpointexception.
0
 
LVL 3

Accepted Solution

by:
sghosh092199 earned 50 total points
ID: 6422423
In that case, you can do the following:

if( (rs.getString("name") != null) && rs.getString("name").equals("tom"))
{
     
}

else
// Null value
0
 

Author Comment

by:lisayan
ID: 6422593
thanks,i got it.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 51
word0 challenge 4 54
create a gui in perl 3 46
eclipse java  build path 6 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now