Solved

Retrieving information from database

Posted on 2002-06-03
10
179 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

930 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

16 Experts available now in Live!

Get 1:1 Help Now