Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

How do I get the column name/value pairs from a ResultSet?

Thread view: 
laredotornado@zipmail.com - 24 Apr 2006 16:16 GMT
Hello,

I'm using JDK 1.3.  If I am iterating through a ResultSet, for a
particular result row, how do I look up all the name/value pairs
without explicitly knowing the column names or the number of columns
returned in the result row?

Thanks, - Dave
Bjorn Abelli - 24 Apr 2006 16:39 GMT
> I'm using JDK 1.3.  If I am iterating through a ResultSet, for a
> particular result row, how do I look up all the name/value pairs
> without explicitly knowing the column names or the number of columns
> returned in the result row?

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSetMetaData.html

Here's an example:

--------------------------------------

import java.sql.*;

class SelectSample
{
 public static void main (String args []) throws SQLException
 {
   DriverManager.registerDriver(new YourDriver());
   String cstring   = "the Connection string";

   Connection conn  =
     DriverManager.getConnection (cstring);

   Statement  stmt  = conn.createStatement ();
   String     query = "the query";

   ResultSet rset   = stmt.executeQuery (query);
   ResultSetMetaData rsMetaData = rset.getMetaData();

   int columnCount = rsMetaData.getColumnCount();

   for (int i = 0; i < columnCount; i++)
   {
     String columnName = rsMetaData.getColumnName(i+1);
     System.out.print(columnName + "\t");
   }

   System.out.println("");

   while (rset.next ())
   {
     for (int i = 0; i < columnCount; i++)
     {
       System.out.print (rset.getString (i+1) + "\t");
     }
     System.out.println ("");
   }

   // Don't forget to clean up...

   rset.close();
   stmt.close();
   conn.close();
 }
}

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.