@RKR好的,这里有您的示例:
Integer[][] myarr ={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} }; String testSection = "Insert Arrays one by one"; stopWatch.start(testSection); myDBconn.setAutoCommit(false); Statement stmt = myDBconn.createStatement(); stmt = myDBconn.createStatement(); stmt.execute("TRUNCATE TABLE DEVDUDE.T3"); // loop over our array of arrays and visit each once for (int i = 0 ; i < (myarr.length); i++) { int curr_length = myarr[i].length; String arrayFunction = "ARRAY ("; for (int j = 0; j < (curr_length); j++){ arrayFunction = arrayFunction.concat(myarr[i][j].toString()) ; // add comma if this is not the last element if (j < (curr_length - 1)){ arrayFunction = arrayFunction.concat(", ") ; } } arrayFunction = arrayFunction + ")" ; // now the arrayFunction should loook like this // ARRAY ( ..., .... ,... ) String insCMD = "INSERT INTO T3 (id, c1) " + " VALUES (" + i + ", " + arrayFunction + " ) "; System.out.println(insCMD); int affectedRows = stmt.executeUpdate(insCMD); System.out.println("Loop round " + i + ", last affected row count " + affectedRows); } myDBconn.commit(); stmt.close(); stmt = null;不,这个代码并 不能 净化输入到INSERT语句,但有以待进行,以避免SQL注入。
当我运行代码时,将输出以下内容:
INSERT INTO T3 (id, c1) VALUES (0, ARRAY (1) ) Loop round 0, last affected row count 1INSERT INTO T3 (id, c1) VALUES (1, ARRAY (1, 2) ) Loop round 1, last affected row count 1INSERT INTO T3 (id, c1) VALUES (2, ARRAY (1, 2, 3, 4, 5) ) Loop round 2, last affected row count 1INSERT INTO T3 (id, c1) VALUES (3, ARRAY (1, 2) ) Loop round 3, last affected row count 1INSERT INTO T3 (id, c1) VALUES (4, ARRAY (1, 2, 3) ) Loop round 4, last affected row count 1
并在表上的SELECT返回:
ID C10 1 1 1, 2 2 1, 2, 3, 4, 53 1, 2 4 1, 2, 3



