在jdbi中以List <string>检索@SqlCall的参数

时间:2018-11-14 07:48:06

标签: java oracle jdbc jdbi

@SqlQuery将选择的答案解析为“列表”。示例:

@SqlQuery("Select a.id from indexes a")
List<String> selectIds();

如何将@SqlCall的参数解析为列表?我不知道应该使用java.sql.Types中的哪种类型。例如此处:

@SqlCall("begin " +
         "SELECT a.id " +
         "BULK COLLECT " +
         "INTO :output " +
         "from indexes a; "
         "end;") 
@OutParameter(name = "output", sqlType = #Don't know what to put here#)
OutParameters selectIds();

UPD。 试过这个:

@SqlCall("begin " +
        "SELECT DISTINCT scu.LOGIN " +
        "BULK COLLECT " +
        "INTO :output " +
        "from sc_users scu; " +
        "end;")
@OutParameter(name = "output", sqlType = Types.ARRAY)
OutParameters selectIds();

知道:

org.jdbi.v3.core.statement.UnableToCreateStatementException: Exception while binding 'output' [statement:"begin SELECT DISTINCT scu.LOGIN BULK COLLECT INTO :output from sc_users scu; end;", rewritten:"begin SELECT DISTINCT scu.LOGIN BULK COLLECT INTO ? from sc_users scu; end;", arguments:{ positional:{}, named:{output:org.jdbi.v3.core.statement.Call$OutParamArgument@45198fd9}, finder:[]}]

UPD2。也尝试过此操作:

@SqlCall("declare " +
        "ar addressVarray; " +
        "begin " +
        "ar := addressVarray('asd'); " +
        ":output := ar; " +
        "end;")
@OutParameter(name = "output", sqlType = Types.ARRAY)
OutParameters getArray();

其中addressVarrayCREATE Or Replace TYPE addressVarray AS VARRAY(2) OF VARCHAR2(50);

再次获得:

org.jdbi.v3.core.statement.UnableToCreateStatementException: Exception while binding 'output' [statement:"declare ar addressVarray; begin ar := addressVarray('asd'); :output := ar; end;", rewritten:"declare ar addressVarray; begin ar := addressVarray('asd'); ? := ar; end;", arguments:{ positional:{}, named:{output:org.jdbi.v3.core.statement.Call$OutParamArgument@46c22d8e}, finder:[]}]

2 个答案:

答案 0 :(得分:0)

请参见此示例,摘自https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.java.doc/src/tpc/imjcc_c0052030.html

Connection con;
CallableStatement cstmt;
ResultSet rs;
java.sql.Array inPhoneData;
…
cstmt = con.prepareCall("CALL GET_EMP_DATA(?,?)");
                                            // Create a CallableStatement object
String[] charArray = new String[] {"a", "b", "c"};        
inPhoneData = conn.createArrayOf("CHAR", charArray);   
cstmt.setArray(1, inPhoneData);             // Set input parameter
cstmt.registerOutParameter (2, java.sql.Types.ARRAY);         
                                            // Register out parameters
cstmt.executeUpdate();                      // Call the stored procedure
Array outPhoneData = cstmt.getArray(2);                                   
                                            // Get the output parameter array
System.out.println("Parameter values from GET_EMP_DATA call: ");
String [] outPhoneNums = (String [])outPhoneData.getArray();
                                            // Retrieve output data from the
                                            // JDBC Array object into a Java
                                            // String array
for(int i=0; i<outPhoneNums.length; i++) {
  System.out.print(outPhoneNums[i]); 
  System.out.println();  
}

答案 1 :(得分:0)

我设法通过 Jdbi Handle 完成了这项工作:

var query = "begin " +
         "SELECT a.id " +
         "BULK COLLECT " +
         "INTO :output " +
         "from indexes a; "
         "end;"

try ( var call = handle.createCall( query )
  .registerOutParameter( "output", java.sql.Types.ARRAY ) ) // see note below
  // .bind( "anotherParam", "value" )
  ) {
  var out = call.invoke();
  return out.getArray( "output" );
}

注意:确保首先在Call返回的createCall接口上注册输出参数!否则你会得到提到的例外。