Link to home
Start Free TrialLog in
Avatar of lisayan
lisayan

asked on

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

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.  
Avatar of sghosh092199
sghosh092199

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!


   
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"))
{
   ...
}
Avatar of lisayan

ASKER

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")){
      ....
   }
That will be fine. In case you are using strName, it will assume different values for each record.

Avatar of lisayan

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of sghosh092199
sghosh092199

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lisayan

ASKER

thanks,i got it.