java数组插入postgres

时间:2014-09-23 19:21:18

标签: java postgresql

我正在尝试将java数组插入postgres并收到以下错误:

  

线程中的异常" main" org.postgresql.util.PSQLException:没有价值   为参数2指定

以下代码:

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

    //This next line throws the error
    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://xxx.xxx.xxx.xxx:xxxx/" + database + "?user=" + 
        userName + "&password=" + password; 
}

2 个答案:

答案 0 :(得分:4)

看第二个ps.setObject()它第二次设置第一个参数。

应该阅读

ps.setObject(1, sku);
ps.setObject(2, cross, java.sql.Types.VARCHAR, cross.length);

答案 1 :(得分:2)

您尚未为PreparedStatement指定第二个参数。您正在重复第一个参数的值两次

ps.setObject(1, sku);
ps.setObject(1, cross, java.sql.Types.VARCHAR, cross.length);

应该是

ps.setObject(1, sku);
ps.setObject(2, cross, java.sql.Types.VARCHAR, cross.length);