1
我有一個用於將ArrayList插入PostgreSQL的Java方法。Java PostgreSQL arrayList <Object[]>插入代碼 - 適用於任何數量的列表
public void postgreSQLInsert(String tblname, String col1, String col2, String col3) throws SQLException, IOException, InterruptedException {
ArrayList<Object[]> insertList = voltdbresultarray;
if (insertList == null) {
} else {
for (Object[] columnValues : insertList) {
try {
System.out.println(columnValues[0]);
System.out.println(columnValues[1]);
System.out.println(columnValues[2]);
String sql = "INSERT INTO " + tblname + "(" + col1 + "," + col2 + "," + col3 + ") " + "VALUES (" + columnValues[0] + ", '" + columnValues[1] + "', " + columnValues[2] + ");";
postgresStmt.executeUpdate(sql);
} catch (PSQLException e) {
System.out.println("Duplicate!");
continue;
}
}
}
}
Col1中,COL2和COL3在別處創建的所有列名字符串,voltdbresultarray是一個靜態數組給定值較早。它一切正常,但只適用於三列的對象數組。我想使它適用於具有任意列數的對象數組,但我無法弄清楚。有沒有辦法讓它需要任意數量的列參數並從中創建正確的SQL查詢?
這幾乎是我正在尋找,是的。什麼是價值SB應該做的?它變紅了。 – Lateral
@ user3779281編輯添加缺少的聲明。 –
啊。那麼,它看起來像在工作。有沒有辦法讓它打印出正確的值,就像以前一樣? – Lateral