Solved

Retrieving information from database

Posted on 2002-06-03
10
182 Views
Last Modified: 2012-05-04
Hello all,

I am trying to do something quite simple in java, retrieve information from a database.  Here are the steps I do:

1 - Declare my variables:
String browserOutput = "";
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

2- Connect to the database
conn = DriverManager.getConnection(bla bla bla) - it works

3- Create a variable of type Statement
stmt = conn.createStatement()

So far, everything works.

And then the following code:

 try {
     rset = stmt.executeQuery("SELECT  a.project_description FROM metric_project a");

     String project_desc = rset.getString(1);                
     while(rset.next()){
     System.out.println(project_desc);
     }
     }
 catch(SQLException e){
     browserOutput = "Error:SQL error of: " + e.getMessage();
     }

And then it does not work.  If I look with the debugger, I can see that the rset is ok.  I am just not able to retrieve the information in rset with that line of code:

String project_desc = rset.getString(1);

and the variable project_desc is always empty.  

What is wrong?

Thanks in advance,
Freesky
0
Comment
Question by:freesky
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
ID: 7052861
You should call rset.getString(1) after rset.next()

                     
    while(rset.next()){
String project_desc = rset.getString(1);
    System.out.println(project_desc);
    }
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7053055
Kotan is correct. The next method moves the ResultSet's internal cursor to the next row returned by the query. Only when the cursor is 'pointing' to a row of the ResultSet can you use the getXXX methods which will return the column values. The way you've coded it is somewhat reminiscent of dynamic bindings in other languages where you would associate a native program variable with the column of a result set and moving the cursor to the next row automatically caused the native variables to be populated with the values of the row. JDBC works backwards of that paradigm. There is no 'binding' or association of specific variables with columns. Values are retrieved by getXXX methods and assigned to program variables explicitly after each cursor-movement method (first, next, prev, last, etc.)

Jim
0
 

Expert Comment

by:udaykumar22
ID: 7053091
hi freesky,

Jim and Kotan have pretty much summed things up.

Please use the following code and things will work prefectly as long as there is some description in the table otherwise it will print 'null'.

===========================================================

try
{
    rset = stmt.executeQuery("SELECT a.project_description FROM metric_project a");

    while(rset.next())
    {
        System.out.println(rset.getString(1));
    }
}
catch(SQLException SQLE)
{
    browserOutput = "Error: SQL Exception : " + SQLE.getMessage();
}

===========================================================

Cheerio,
Uday.

0
Technology Partners: 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!

 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7053957
Not nice :-(
Kotan should get the points.
0
 

Author Comment

by:freesky
ID: 7054893
Thanks udaykumar22 for your answer, it is very appreciated.  However, I would like to give the points to Kotan, who answered first.  Have a nice day!
0
 

Author Comment

by:freesky
ID: 7054894
Thanks udaykumar22 for your answer, it is very appreciated.  However, I would like to give the points to Kotan, who answered first.  Have a nice day!
0
 

Author Comment

by:freesky
ID: 7054899
Thanks udaykumar22 for your answer, it is very appreciated.  However, I would like to give the points to Kotan who answered first.  Have a nice day, Freesky
0
 

Author Comment

by:freesky
ID: 7054904
Thanks a lot Kotan for your answer.  Also, a note to jim_cakalic for his very clear and detailed information.  Have a nice day!
0
 

Expert Comment

by:udaykumar22
ID: 7055379
Hi freesky,

I just tried to help here.

Jim,

I am sorry to hav put mine comment as an answer. Shall keep this in mind. It was not intended as an answer anyway.

Thanx for trying to help.

Regards,
Uday.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7056714
No problem :-)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 103
How to execute a Python program and gather return output in Java 2 50
eclipse buid path vs tomcat lib path 10 38
java example issue 5 46
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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