Please send questions to st10@humboldt.edu .
//
// playing with just a FEW of the ResultSet metatdata
// possibilities...
//
//
// NOTE: this WILL NOT WORK unless you add 
//       /home/univ/oracle/products/jdbc/lib/classes12.zip
// ...to your CLASSPATH!!!
//
// modified by: Sharon Tuttle
// last modified: 11-16-00

import java.sql.*;

public class TryRSMetaData1
{
	public static void main(String args[])
	{
		DatabaseMetaData	dbMetaData;
		ResultSetMetaData	rsMetaData;
		PreparedStatement	myPStmt;
		Statement		myStmt;
		ResultSet		myResultSet;
		String			myQuery, colLabel, wholeRow;
		String			colValue;
		int			numCols, maxNumConns;
		int			padAmt, i, numColsShow;
		int			colSize[], diff;

		try
		{
                    	// load driver, set up connection
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection con = DriverManager.getConnection
            			("jdbc:oracle:thin:@redwood:1521:student",
             			 "java", "java");

			// let's create a statement object, set up a
			// table query, and execute it --- I
			// want to get a ResultSet to get information
			// about!
			myStmt = con.createStatement();
			myQuery = "select * from emp";
			myResultSet = myStmt.executeQuery(myQuery);

			// using the ResultSet, much metadata about
			// a particular ResultSet is available;
			// this is how you get a ResultSet's 
			// ResultSetMetaData object:
			rsMetaData = myResultSet.getMetaData();

			System.out.println("------------------");
			System.out.println("in the table EMP: ");
			System.out.println("------------------");

			// want to be able to loop through the
			// columns in a ResultSet? Then you need
			// to know how many there are: 
			numCols = rsMetaData.getColumnCount();
			System.out.println("number of columns is: " +
				numCols);			

			System.out.println("------------------");

			// print column headings? until hit 80 characters 
			// wide...
			wholeRow = "";
			colSize = new int[numCols];
			for (i = 1; i <= 8; i++)
			{
				colLabel = rsMetaData.getColumnLabel(i);
				colSize[i] = 
					rsMetaData.getColumnDisplaySize(i);
                                                                               
				// if size of current wholeRow + next column's
				// size is greater than 80, break
				// out of loop;
				if ((wholeRow.length() + colSize[i]) > 80)
				{
					i = i-1;   // how many cols I got
					break;
				}
			
				wholeRow = wholeRow + colLabel;

				// if colSize is wider than its label,
				// pad with that many blanks;
				if (colSize[i] > colLabel.length())
				{
					padAmt = colSize[i] - 
						colLabel.length();
					for (int j=0; j < padAmt; j++)
					{
						wholeRow = wholeRow + " ";
					}
				} 

				// now, put a blank before next column
				wholeRow = wholeRow + " ";
			}
				
			numColsShow = i;		
			if (numColsShow < numCols)
			{
				System.out.println("Only printing first " +
					numColsShow + " rows; too wide " +
					"otherwise.");
				System.out.println("----------------");
			}
			System.out.println(wholeRow);

			// underline column headings
			for (i=0; i < wholeRow.length(); i++)
			{
				System.out.print("-");
			}
			System.out.print("\n");

			// now, I could use more resultset metadata
			// to get types of each column, etc., but
			// I'm going to wimp out and just get
			// these as String's...
			while (myResultSet.next())
			{
				wholeRow = "";
				for (i=1; i <= numColsShow; i++)
				{
					colValue = myResultSet.getString(i);

					// what if colValue is null?
					// (you'll get NullPointerException
					// if you try to ck its length())
					if (colValue == null)
					{
						// set it to the
						// empty string,
						// to cause wholeRow
						// to be padded below...
						colValue = " ";
					}
					// oh, just chop the %^$%^ thing
					// off if its string version is
					// too wide!!!
					else if (colValue.length() >
						colSize[i])
					{
					   colValue = colValue.substring(
						0, colSize[i]-1);
					}
					wholeRow = wholeRow + colValue;
					
					// pad with blanks to column size
					if (colValue.length() < colSize[i])
					{
						diff = colSize[i] -
							colValue.length();
						for (int j=0; j<diff; j++)
						{
							wholeRow = wholeRow
								+ " ";
						}
					}
		
					// now add the 1 blank in between
					wholeRow = wholeRow + " ";

				}
				System.out.println(wholeRow);
			}
			
			// put lines under last row
			for (i=0; i < wholeRow.length(); i++)
			{
				System.out.print("-");
			}
			System.out.println("\n\n");


			// do not forget to close the Statement
			// and connection when done!
			myStmt.close();
			con.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}