drno-reg
@drno-reg
см не кратко

Как вывести значения курсора из pl/sql блока Oracle?

Здравствуйте.

Пытаюсь вывести значения курсора из pl/sql блока Oracle

в pl-sql developer дает такой результат

SUBARU|24|9
AUDI|10|2
LADA|0|0
Итого|34|11


на Java пробую так, чтобы вывести значения хотя бы какого-то одного столбца.

try
{
	final Connection c = DriverManager.getConnection(Connection_URL, user, password);

        plsql="DECLARE\n" +
"  v_ref  SYS_REFCURSOR;\n" +
"  v_prt varchar2(20) default 'Итого' ;\n" +
"BEGIN\n" +
"    REPORTS.mi_inwork(v_prt, v_ref);\n" +
"   \n" +
"? := v_ref;\n" +
"END;";

        CallableStatement cs = c.prepareCall(plsql);
        cs.registerOutParameter(1, OracleTypes.CURSOR);
        cs.execute();

        ResultSet cursorResultSet = (ResultSet) cs.getObject(1);

        while (cursorResultSet.next ())
        {
           out.println (cursorResultSet.getString(1));
        }
        cs.close();
        c.close();
	}
catch (Exception e) {
out.println( "<h1>exception: "+e.getMessage()+"</h1>" );
}


на этапе

while (cursorResultSet.next ())
        {
           out.println (cursorResultSet.getString(1));
        }


возвращается ошибка NULL

Подскажите пожалуйста где ошибка и как ее решить?

p.s.

попробовал еще таким образом

Connection dbConnection = null;
		CallableStatement callableStatement = null;

		String getDBUSERByUserIdSql = "{call REPORTS.mi_inwork(?,?)}";

	try {
			dbConnection = getDBConnection(Driver_Class, Connection_URL, UserName, Password);
			callableStatement = dbConnection.prepareCall(getDBUSERByUserIdSql);

			callableStatement.setString(1, "Итого");
			callableStatement.registerOutParameter(2, OracleTypes.CURSOR);
		//	callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
		//	callableStatement.registerOutParameter(4, java.sql.Types.DATE);

			// execute getDBUSERByUserId store procedure
			callableStatement.executeUpdate();

			String Name = callableStatement.getString(1);
			String CountRequest1 = callableStatement.getString(2);
			///Date createdDate = callableStatement.getDate(4);

			System.out.println("Name : " + Name);
			System.out.println("CountRequest1 : " + CountRequest1);
		//	System.out.println("CreatedDate : " + createdDate);

		} catch (SQLException e) {

			//System.out.println(e.getMessage());
			out.println( "<h1>exception: "+e.getMessage()+"</h1>" );

		} finally {

			if (callableStatement != null) {
				callableStatement.close();
			}

			if (dbConnection != null) {
				dbConnection.close();
			}
  • Вопрос задан
  • 1621 просмотр
Решения вопроса 2
al_gon
@al_gon
Непонятно , что у вас в plsql проиходит. Процедура ведь уже лежит в ДБ.
https://www.mkyong.com/jdbc/jdbc-callablestatement...
Ответ написан
drno-reg
@drno-reg Автор вопроса
см не кратко
Рабочий вариант будет выглядеть так

Connection conn = null;
CallableStatement stmt = null;
ResultSet rset = null;

String SOME_NAME = "{call REPORTS.mi_inwork(?,?)}";

	try {
        conn = DriverManager.getConnection(Connection_URL,UserName, Password);

		        stmt = conn.prepareCall(SOME_NAME);//We have declared this at the very top
        stmt.setString(1, "Итого");//Passing CompanyID here
        stmt.registerOutParameter(2, OracleTypes.CURSOR);//Refcursor selects the row based upon query results provided in Package.
        stmt.execute();
        rset = (ResultSet) stmt.getObject(2);

        while (rset.next()) {
            out.println(rset.getString(1)+" | "+ rset.getString(2)+" | "+ rset.getString(3));
}
} catch (Exception e) {
     //   LOGGER.error("Error extracting ", e);
        out.println( "<h1>exception: "+e.getMessage()+"</h1>" );
} finally {
     //   DBUtils.cleanUp(conn, stmt, rset);
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы