Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-08-23
7
Medium Priority
?
379 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 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