Solved

Retrieving information from database

Posted on 2002-06-03
10
180 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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