• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Retrieving information from database

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
freesky
Asked:
freesky
  • 4
  • 3
  • 2
  • +1
1 Solution
 
kotanCommented:
You should call rset.getString(1) after rset.next()

                     
    while(rset.next()){
String project_desc = rset.getString(1);
    System.out.println(project_desc);
    }
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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
 
udaykumar22Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jim CakalicSenior Developer/ArchitectCommented:
Not nice :-(
Kotan should get the points.
0
 
freeskyAuthor Commented:
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
 
freeskyAuthor Commented:
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
 
freeskyAuthor Commented:
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
 
freeskyAuthor Commented:
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
 
udaykumar22Commented:
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
 
Jim CakalicSenior Developer/ArchitectCommented:
No problem :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now