0

I'm trying to get the result of my Result Set where I register my 2nd parameter as INTEGER. From what I read I should get the object first before retrieving it using Result Set.

Code

try (Connection myConn = DBUtil.connect();
             CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
        {
             myFirstCs.setString(1, searchSection);
             myFirstCs.registerOutParameter(2, Types.INTEGER);
             myFirstCs.executeUpdate();

             String name = null;

             System.out.println(myFirstCs.getObject(2));

            ResultSet myRs = null;
            myRs = (java.sql.ResultSet)myFirstCs.getObject(2);
            while (myRs.next()) // Retrieve result set rows
            {                  
                name=myRs.getString(1);
                System.out.print("Section Name: "+name);
            }
            myRs.close(); 

Is this the proper way retrieving Result Set? I try this method using try with resource. I missed something here ResultSet myRs = myFirstCs.getObject(2, type) don't know what will I put in my 2nd parameter?

Sample Only

try (ResultSet myRs = myFirstCs.getObject(2, type))//What should I put here?
              {
                  while (myRs.next())
                  {
                      name = myRs.getString(1);
                      System.out.print("Section Name: "+name);
                  }//end of while
              }

Feel free to comment. Any help would appreciate. Thank you.

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • 1
    isnt `myFirstCs.getInt(2)` working ?? – Rahman Mar 26 '16 at 07:36
  • The second argument will be the sqlType you can check it from oracle doc...https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html sqlType - the JDBC type code defined by java.sql.Types. If the parameter is of JDBC type NUMERIC or DECIMAL, the version of registerOutParameter that accepts a scale value should be used. – Ajay Pandya Mar 26 '16 at 07:37
  • Seriously, this is, again, a duplicate, slightly reworded, of your earlier questions. Do I need to keep closing them every day? – Mark Rotteveel Mar 26 '16 at 08:52

1 Answers1

0

@Mia following two answers, I think one of them should work-

try (Connection myConn = DBUtil.connect();
         CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
    {
         myFirstCs.setString(1, searchSection);
         myFirstCs.registerOutParameter(2, Types.INTEGER);
         myFirstCs.executeUpdate();

         String name = myFirstCs.getObject(2);
            System.out.print("Section Name: "+name);
    }

OR

try (Connection myConn = DBUtil.connect();
         CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
    {
         myFirstCs.setString(1, searchSection);
         myFirstCs.registerOutParameter(2, Types.INTEGER);
         myFirstCs.executeUpdate();

         String name = null;

         System.out.println(myFirstCs.getObject(2));

        ResultSet myRs = null;
        myRs = myFirstCs.getResultSet();
        while (myRs.next()) // Retrieve result set rows
        {                  
            name=myRs.getString(1);
            System.out.print("Section Name: "+name);
        }
        myRs.close(); 
Ashu
  • 71
  • 10