我試圖插入一個字符串數組到Postgres中。我從Postgres中得到一個無效的類型錯誤。錯誤設置java字符串[] postgres準備好的語句
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;
}
但是你有一個問號和一個值的數組。您可以嘗試一串逗號分隔值。 – 2014-09-23 15:33:59
JDBC API不能以這種方式工作。請描述你想要達到的目標。最重要的是:PostgreSQL不支持'NVARCHAR'類型afaik – 2014-09-23 16:06:50
ps.setObject(1,skus,java.sql.Types.VARCHAR,skus.length); //修復錯誤,將其作爲答案發布。 – Voltron 2014-09-23 19:10:08