Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009
    Posts
    26

    Unanswered: Having problem with iterating throug the resultSet

    Hello Everyone,
    I have a question on DB2 with java. Select * from mySchemaName.myTableName this is the query i am using to retrieve all the rows of the table.Now i don have any idea of the table .I dont know how many columns are there and how to retrieve the column values.


    By using the following two methods ResultSetMetaData.getColumnName and ResultSetMetaData.getcolumnClassName() i am getting the column name and column data type in java.
    Is there any way to get the values of the resultSet in a generic way.I mean ,to get each and every value in the result.I wanted to use ResultSet.getString() or ResultSet.getBoolean() based on the column type but before to that i have to check each and every columns data type and then use the respective methods.Is there any generic way with out checking the column data type we can retrieve the values.


    I would really appreciate if some one can reply me.

    Thanks in advance.
    swati

  2. #2
    Join Date
    Dec 2008
    Posts
    76
    I have done what you are doing, and in my experience there is no magic process that will eliminate the need to know how many columns you are dealing with and what data type each column is.

    Maybe someone else has a different experience, but I doubt it. There is third party code and objects already written that will do most of the effort for you. Try looking in developerWorks on IBM's website.
    RD

  3. #3
    Join Date
    Jul 2009
    Posts
    26
    Thanks a lot for your response.
    I am getting each and every columns data type and checking with all java to sql data types and then getting the value.the code may some thing look like
    ResultSetMetaData.getcolumncount()
    for(int i=1;i<ResultSetMetaData.getcolumncount();i++){
    String columntype=ResultSetMetaData..getColumnClassName(i );
    String columnName=ResultSetMetaData.getColumnName(i);

    if(columntype.equlas(java.lang.String){
    String value=(String)ResultSet.getString(columnName);//like this i am checking with all java to sql data types.

    }


    }
    Is there any good approach apart from the above code...

    I would really appreciate if some one can answer this question....


    Thanks,
    swati

  4. #4
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    Quote Originally Posted by swati malla
    Is there any way to get the values of the resultSet in a generic way.I mean ,to get each and every value in the result.
    What's wrong with ResultSet.getObject() ?

  5. #5
    Join Date
    Jul 2009
    Posts
    26
    if we go with ResultSet.getObject().Again we have to convert it into to some data types to retrieve the value right?
    the code looks some what
    Object ob=ResultSet.getObject()
    if(columntype.equlas(java.lang.String){
    String value=(String)ob

    }

    I guess thsi is the way but no effective way am i correct i might be wrong though...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •