我有一个对象的arraylist并试图将列表插入HANA。所以我的插入代码看起来像
PreparedStatement stmt = conn
.prepareStatement("INSERT INTO SCHEMA.TABLE VALUES"
+ " (?, ?, ?, ?, ?, ?, ?, ARRAY("+"1,2,3"+")");
for (int i = 1; i <= ITERATION_MAX; i++) {
stmt.setInt(1, listofdata.get(i).get_id());
stmt.setInt(2, listofdata.get(i).get_name());
stmt.setInt(3, listofdata.get(i).get_place());
stmt.setInt(4, listofdata.get(i).get_year());
stmt.setInt(5, listofdata.get(i).get_day());
stmt.setInt(6, listofdata.get(i).get_rollno());
stmt.setInt(7, listofdata.get(i).get_main_subject());
stmt.setArray(8, listofdata.get(i).get_elective());
stmt.addBatch();
}
stmt.executeBatch();
下面
listofdata.get(i).get_elective()
返回一个整数数组。
但这不起作用。根据我的程序ARRAY函数每次调用但为什么不插入HANA Database.So过了一段时间我明白我必须将JAVA数组转换为HANA数组。我怎么能转换一个Java数组到HANA数组。 任何帮助表示赞赏。
答案 0 :(得分:2)
@RKR好的,这里有你的例子:
/*
* We're goin to insert several arrays into the HANA table,
* with different lengths
*
* let's assume we already got a table like this
* CREATE COLUMN TABLE T3 ( ID INT PRIMARY KEY, C1 INT ARRAY );
*/
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 1
INSERT INTO T3 (id, c1) VALUES (1, ARRAY (1, 2) )
Loop round 1, last affected row count 1
INSERT INTO T3 (id, c1) VALUES (2, ARRAY (1, 2, 3, 4, 5) )
Loop round 2, last affected row count 1
INSERT INTO T3 (id, c1) VALUES (3, ARRAY (1, 2) )
Loop round 3, last affected row count 1
INSERT INTO T3 (id, c1) VALUES (4, ARRAY (1, 2, 3) )
Loop round 4, last affected row count 1
表上的SELECT返回:
ID C1
0 1
1 1, 2
2 1, 2, 3, 4, 5
3 1, 2
4 1, 2, 3
答案 1 :(得分:1)
这不是标准代码,但仍然可以正常工作。希望它可以帮助任何人。
for (int i = 1; i <= ITERATION_MAX; i++) {
String arraylist=Arrays.toString(listofdata.get(i).get_arraylist.replace("[","").replace("]",""));
id=listofdata.get(i).get_id();
name= listofdata.get(i).get_name();
place=listofdata.get(i).get_place();
year= listofdata.get(i).get_year();
day=listofdata.get(i).get_day();
rollno= listofdata.get(i).get_rollno();
main_subject= listofdata.get(i).get_main_subject();
elective= listofdata.get(i).get_elective();
Statement stmt = conn.createStatement();
String sql="INSERT INTO SCHEMA.TABLE values("+
+name+","
place+","
year+day+","
rollno+","
main_subject+","
elective"+","
"ARRAY("+arraylist+")" ;
stmt.addbatch(sql);
}
stmt.executeBatch();
stmt.close();
conn.commit();
conn.close();