Solved

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

Posted on 2001-08-23
7
340 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

867 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

21 Experts available now in Live!

Get 1:1 Help Now