Solved

Result set won't return value

Posted on 2006-07-16
6
235 Views
Last Modified: 2010-03-31
Hi all

I have a really weird problem.

I have a java file that I'm trying to instantiate from values in a SQL Server database table.

I can connect to the database ok and start getting values from the view that I'm interested:

        try
        {      
              String strSQL = "SELECT * FROM qryCompliance_Officers WHERE Name_User = '" + strUsername + "'";
              Statement stmt = null;
              stmt = dbConnLogin.createStatement();
              ResultSet rs = null ;
              rs = stmt.executeQuery(strSQL);                
                  
                  rs.next();                        
                  
                  this.strName_User = strUsername;
                  this.strF_Name = rs.getString("F_Name");             
                  this.strL_Name = rs.getString("L_Name");
                  this.strFull_Name = rs.getString("Full_Name");
                  this.lngPosition_ID = rs.getLong("Position_ID");
                  this.strPosition = rs.getString("Position");

As you can see I have read a value from the resultset a total of 6 times.

The weird thing is as soon as I try to read another value:

this.lngOffice_ID = rs.getLong("Office_ID");

it throws a SQLException error with an error value of 0.  The column I am interested in exists, I even used the findColumn method of resultset to confirm it is there.  Similarly if I place:

this.lngOffice_ID = rs.getLong("Office_ID");

at the top of the list i.e.

                                                this.lngOffice_ID = rs.getLong("Office_ID");
                  this.strName_User = strUsername;
                  this.strF_Name = rs.getString("F_Name");             
                  this.strL_Name = rs.getString("L_Name");
                  this.strFull_Name = rs.getString("Full_Name");
                  this.lngPosition_ID = rs.getLong("Position_ID");
                  this.strPosition = rs.getString("Position");

that statement executes ok but then an error is thrown at the last line:

this.strPosition = rs.getString("Position");

It is as if all I can do is call something from the recordset a total of six times before it refuses to do any more.

Any help appreciated, I am completely at a loss.

Phil
0
Comment
Question by:pmccar06
[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
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 17117145
You need to call rs.next in a loop and only retrieve values if it returns true:


ResultSet rs = stmt.executeQuery(strSQL);              
while (rs.next()) {
    // Now get them
}                    
0
 
LVL 8

Accepted Solution

by:
koppcha earned 50 total points
ID: 17117517
Please try this
1> Instead  of "SELECT * FROM qryCompliance" try SELECT F_Name,L_Name,...
list all the columns you want to retrieve.
2>When the SQLException is thrown could you try to get the exact message it is throwing
catch(SQLException e) {
System.out.println(e.getMessage());
}
0
 
LVL 10

Assisted Solution

by:mukundha_expert
mukundha_expert earned 35 total points
ID: 17119711

Use ResultSetMetaData to find the number of columns its returning.
also check whether all the columns are returning,
ResultSetMetadata rms = rs.getMetaData () ;
for ( int i = 0 ; i < rms.getColumnCount() ; i ++ )
System.out.println (  rms.getColumnLabel ( i )  );


0
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.

 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 17120641
> You need to call rs.next in a loop and only retrieve values if it returns true:

unnecessary, and a row is already being returtned anyway

you could add an if, but it won't fix your probl;em.

               if (rs.next()) {
                  this.strName_User = strUsername;
                  this.strF_Name = rs.getString("F_Name");          
                  this.strL_Name = rs.getString("L_Name");
                  this.strFull_Name = rs.getString("Full_Name");
                  this.lngPosition_ID = rs.getLong("Position_ID");
                  this.strPosition = rs.getString("Position");

Not sure whats the cause of your problem, perhaps try a different driver. your code looks fine
0
 

Author Comment

by:pmccar06
ID: 17128357
Hi all

It turned out that my problem was something to do with the order that I was getting the column values.  If I changed by code so that the values were read in the order in which they appear in the query (view), then the problem dissapeared.  Don't ask me why, still weird, but solved the issue.

I tried to split the points the best way I could.  koppcha you post helped me the most of all, especially using the getMessage method from the error object.

Thanks all for your help.

Cheers Phil
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17128405
:-)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

729 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