如何在循环中将数组插入数据库 [英] How to insert an array in a loop to the database

查看:44
本文介绍了如何在循环中将数组插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在循环中将一组数组插入数据库(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!)

这篇关于如何在循环中将数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆