oracle-databasejdbcmetadatacallable-statement

Oracle JDBC callable statement and getMetadata count


Let's say I have a pl/sql like below:

  final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "manager");
    String plsql = "" +
    " declare " +  
    "    p_id varchar2(20) := null; " +
    "    l_rc sys_refcursor;" +
    " begin " +
    "    p_id := ?; " +
    "    ? := 'input parameter was = ' || p_id;" +
    "    open l_rc for " +
    "        select 1a id, 'hello' name from dual " +
    "        union " +
    "        select 2a, 'peter' from dual; " +
    "    ? := l_rc;" +
    " end;";

    CallableStatement cs = c.prepareCall(plsql);
    cs.setString(1, "12345");
    cs.registerOutParameter(2, Types.VARCHAR);
    cs.registerOutParameter(3, OracleTypes.CURSOR);

    cs.execute();

Everything's fine here but how can I get a metadat.getColumnCount() from this?

What I would like to achive? Instead of:

 ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
        while (cursorResultSet.next ())
        {
            System.out.println (cursorResultSet.getString(1) + " " + cursorResultSet.getString(2));
        } 

I would like to find metadata count and print all values for one record in a loop like:

//pseudo code

int x = callablestatement.metadata.count();
String myRecord='';
while (cursorResultSet.next ()){
        for(int i=1; i<=x; i++){
          myRecord= myRecord+ " " + cursorResultSet.getString(i);
                               }
                               }

Solution

  • To obtain the number of columns in the result set, you need to use

    cursorResultSet.getMetaData().getColumnCount()