Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Declaración invocable - PostgreSQL - Parámetros de salida múltiples

Tenemos un procedimiento almacenado en una base de datos PostgreSQL que toma múltiples parámetros de entrada y salida. La llamada al procedimiento del cliente PG Admin funciona bien cuando hacemos lo siguiente,

llamar a proc1(entrada1, entrada2, salida1, salida2)

Sin embargo, si intentamos realizar esta llamada a través de JDBC CallableStatement, obtenemos el siguiente error:

 org.postgresql.util.PSQLException: This statement does not declare an OUT parameter. Use { ?= call ... } to declare one. at org.postgresql.jdbc.PgCallableStatement.registerOutParameter(PgCallableStatement.java:205)

El controlador PostgreSQL es "org.postgresql.Driver"

La versión del controlador es postgressql-42.2.5.jar

¿Cómo hacemos una llamada al procedimiento PostgreSQL que tiene múltiples parámetros de salida de JDBC?

A continuación encontrará el fragmento de código,

 public static void main(String args[]) throws SQLException { Connection conn = null; try { String url = "jdbc:postgresql://<<hostname>>:<<port>>/<<DB>>"; Class.forName("org.postgresql.Driver"); Properties props = new Properties(); props.setProperty("user", "<<user>>"); props.setProperty("password", "<<pass>>"); conn = DriverManager.getConnection(url, props); CallableStatement cs = conn.prepareCall("call schema.proc1(?,?,?,?)"); cs.setString(1, "test"); cs.setInt(2, 1000); cs.registerOutParameter(3, Types.INTEGER); cs.registerOutParameter(4, Types.VARCHAR); cs.execute(); } catch (Exception e) { log.error(e); } finally { if (conn != null) { conn.close(); } } }

A continuación se muestra la versión de muestra del Procedimiento

 Procedure proc1 is (input1 IN varchar2(10), input2 IN number, output1 OUT number, output2 OUT varchar2(10)) IS BEGIN output2 := input1; output1 := input2; END;
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Estoy probando la llamada al procedimiento con varios parámetros OUT en JDBC con PostgreSQL 14.1 y el controlador 42.2.20.

Se debe tener cuidado, ya que la llamada ANSI falla

 cn.prepareCall("{call proc1(?,?,?,?)}")

con org.postgresql.util.PSQLException: ERROR: proc1(character varying, integer) is a procedure Hint: To call a procedure, use CALL. Esto puede estar conectado con esta respuesta.

Similar falla también PostgreSQL CALL cn.prepareCall("call proc1(?,?,?,?)") con

Caught: org.postgresql.util.PSQLException: ERROR: invalid input syntax for type integer: "null" . Esto sugiere un problema con el parámetro OUT de entero null .

Finalmente lo entiendo con un pequeño truco definiendo el parámetro integer como INOUT y pasando cero.

Procedimiento

 create or replace PROCEDURE proc1(input1 IN varchar(10), input2 IN integer, output1 INOUT integer, output2 OUT varchar(10)) LANGUAGE plpgsql AS $$ BEGIN output2 := input1; output1 := input2; END; $$;

JDBC

 // procedure call with two OUT parameter stmt = cn.prepareCall("call proc1(?,?,?,?)") stmt.setString(1,'x') stmt.setInt(2,100) stmt.setInt(3,0) stmt.registerOutParameter(3,Types.INTEGER) stmt.registerOutParameter(4,Types.VARCHAR) stmt.execute() println stmt.getInt(3) println stmt.getString(4)

devuelve como se esperaba

 100 x
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!