Solved

Retrieving information from database

Posted on 2002-06-03
10
178 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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.

705 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

18 Experts available now in Live!

Get 1:1 Help Now