Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am trying to insert a java Array into postgres and get the following error:

Exception in thread "main" org.postgresql.util.PSQLException: No value specified for parameter 2

Code below:

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; 
}
share|improve this question
    
There is a typo. You are setting first parameter twice. – lifus Sep 23 '14 at 19:23

2 Answers 2

up vote 2 down vote accepted

Look at the second ps.setObject() it's setting the first parameter a second time.

It should read

ps.setObject(1, sku);
ps.setObject(2, cross, java.sql.Types.VARCHAR, cross.length);
share|improve this answer

You haven't specified the second parameter for the PreparedStatement. You are repeating the value for first parameter twice

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

should be

ps.setObject(1, sku);
ps.setObject(2, cross, java.sql.Types.VARCHAR, cross.length);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.