Solved

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

Posted on 2001-08-23
7
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

705 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