How can I set a String[] parameter to a native query?

前端 未结 4 2176
野的像风
野的像风 2020-12-17 22:44

This is my PostgreSQL function:

salvarArquivoGeometricoCasoZeroPOINT
(dimensao text,tableName text,tuplas text[],srid text)

It has a

4条回答
  •  死守一世寂寞
    2020-12-17 23:21

    I am very late to answer it.

    This solution is kind of workaround using postgreSQL built-in function, which definitely worked for me.

    reference blog

    1) Convert String Array to Comma Separated String

    If you are using Java8, it's pretty easy. other options are here

    String commaSeparatedString = String.join(",",stringArray); // Java8 feature
    

    2) PostgreSQL built-in function string_to_array()

    you can find other postgreSQL array functions here

    // tableName ( name text, string_array_column_name text[] )
    
    String query = "insert into tableName(name,string_array_column_name ) values(?, string_to_array(?,',') )";
    
    
    int[] types = new int[] { Types.VARCHAR, Types.VARCHAR};
    
    Object[] psParams = new Object[] {"Dhruvil Thaker",commaSeparatedString };
    
    jdbcTemplate.batchUpdate(query, psParams ,types); // assuming you have jdbctemplate instance
    

提交回复
热议问题