We help IT Professionals succeed at work.

Iterate through db2 field names

dbeayon asked
Medium Priority
Last Modified: 2012-05-06
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?

Watch Question


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)
			throw new Exception("Input record not found.");

Open in new window

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 ); 

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Kent OlsenData Warehouse / Database Architect

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,
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.