Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Result set won't return value

Posted on 2006-07-16
6
Medium Priority
?
237 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 40 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 100 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 70 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 92

Assisted Solution

by:objects
objects earned 40 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

688 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