如何在循环中将数组插入数据库
我想在循环中将一组数组插入数据库(HANA).我的代码如下:
I want to insert a set of arrays into a database(HANA) in a loop.My code is below:
public class jdemo {
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:sap://myhdb:30715/?autocommit=false",myname,mysecret);
} catch (SQLException e) {
System.err.println("Connection Failed. User/Passwd Error?");
return;
}
if (connection != null) {
try {
int [] array=new int []{1,2,3};
Array array1= connection.createArrayof("Integer",array)
System.out.println("Connection to HANA successful!");
String sql="INSERT INTO TABLE1 VALUES(1,ARRAY(?))"
PreparedStatement stmt = connection.createStatement(sql);
stmt.setArray(int,array1);
stmt.executeUpdate(sql);
} catch (SQLException e) {
System.err.println("Query failed!");
}
}
}
}
但这不起作用.我试过了
But this does not work. I tried with
Object [] array=new Object []{1,2,3};
不支持此返回方法创建连接数组.
This returned method create array of Connection is not supported.
我的表架构看起来像
ID MARK
__ ____
10 {1,2,3}
11 {3,2,3}
12 {9,2,3}
13 {10,2,3}
14 {12,24,3}
18 {1,27,3}
我也希望我的数据类型为整数数组.感谢任何帮助.
I also want my data type as an integer array.Any help is appreciated.
推荐答案
ARRAY 插入 HANA"的话题已经在这里讨论过几次了.HANA 仅支持通过 ARRAY() 函数存储数组.该函数不以列表为参数,仅以单独的元素为参数.
The topic of "ARRAY inserts into HANA" has been discussed a couple of times here on SO already. HANA only supports to store arrays via the ARRAY() function. This function does not take a list as the parameter, but only separate elements.
所以,而不是
String sql="INSERT INTO TABLE1 VALUES(1,ARRAY(?))"
你必须写
String sql="INSERT INTO TABLE1 VALUES(1, ARRAY( 1, 2, 3))"
对于 JDBC 驱动程序:HANA JDBC 不会自动将 JAVA 数组处理为 HANA 数组 - 这是开发人员必须手动执行的操作.(是的,这不太好,我知道).
For the JDBC driver: HANA JDBC doesn't automatically handle JAVA arrays to HANA arrays - that's something the developer will have to do manually. (yes, it's not nice, I know).
简而言之:目前(HANA 1.0 SP12)数组基本上可以在内部使用(在存储过程中),但它们不是一等公民数据类型.(<- 这是我的看法!)
In short: currently (HANA 1.0 SP12) arrays can basically be used internally (within a stored procedure), but they are not first-class-citizen data types. (<- that's my opinion!)
相关文章