Iterate through db2 field names

In Java 1.6, currently I am hard coding the field values that exist in a db2 table in a Is there a method that will allow me to connect to the db2 table and iterate through the current field names?



dbeayonAsked:
Who is Participating?
 
dbeayonAuthor Commented:
this served my purpose
		ResultSetMetaData rsmd = rsInput.getMetaData();
		int colCount = rsmd.getColumnCount();
	      String colName, colType;
	      for (int i = 1 ; i <= colCount ; i++)
	      {
	        colName = rsmd.getColumnName(i);
	        colType = rsmd.getColumnTypeName(i);
	        addFldsFromInput( colName ); 
	        System.out.println(colName);
	      }

Open in new window

0
 
dbeayonAuthor Commented:
here is the code that I am using to grab the table info
I should be able to iterate through rsInput and list the column names.  Just not sure how at this point

	protected void createRSInput_new() throws Exception
	{
		
		String sql 	= "	Select * from " + TblInput 
					+ " Where DB2Recid = " + iDB2RecId;					
		
		Statement stmt = this.connJob.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		rsInput = stmt.executeQuery( sql);
		
		if( rsInput == null || rsInput.absolute(1) == false)
		{
			System.out.println(sql);
			throw new Exception("Input record not found.");
			}
		return;
		}
	

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi dbeayon,

I missed your question as I was literally "out to lunch".  :)

Your solution should work fine and is generic enough that it should work with just about and database.  If you need extended information from DB2 about a column (or even the column list) you can query the SYSIBM.TABLES view and interrogate the results.

  SELECT * FROM SYSIBM.TABLES where table_name = 'MYTABLE';   -- Note, tables are usually in UPPER case.


Good Luck,
Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.