Iterate through db2 field names

Posted on 2009-02-16
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?

Question by:dbeayon

    Author Comment

    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


    Accepted Solution

    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

    LVL 45

    Expert Comment

    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,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
    Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    737 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

    17 Experts available now in Live!

    Get 1:1 Help Now