Setting java error Line [] for rendered postgres report

I am trying to insert Array of Strings into Postgres. I am getting an invalid Postgres type error.

public static void main(String[] args) throws SQLException {
    String[] skus = { "0514", "0414", "0314", "0214", "0114", "1213", "1113", "1013", "0913", "0813", "0713", "0613" };
    String sqlString = "Insert into dbo.Inventory_Metrics skus values(?)";
    Connection conn = DriverManager.getConnection(getPostgresConnUrl());
    PreparedStatement ps = conn.prepareStatement(sqlString);

    //THIS NEXT LINE THROWS AN ERROR

    ps.setObject(1, skus, java.sql.Types.NVARCHAR, skus.length);
    int status = ps.executeUpdate();
    ps.close();

    System.out.print(status);
}
public static String getPostgresConnUrl() {
    String database = "mycode";
    String userName = "xxxxxxxx";
    String password = "xxxxxxxx";
    return "jdbc:postgresql://192.168.0.50:5432/" + database + "?user=" + userName + "&password=" + password;

}

      

+3


source to share


1 answer


You should use the JDBC Array API in the documentation.

You cannot just setObject

array in JDBC. It would be nice if this were the case, but it is not. You have to handle arrays specifically.



Array jdbcSkus = con.createArrayOf("VARCHAR", skus);
pstmt.setArray(2, jdbcSkus);

      

+6


source







All Articles