How to insert an array in a loop to the database(如何在循环中将数组插入数据库)
问题描述
我想在循环中将一组数组插入数据库(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!)
这篇关于如何在循环中将数组插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在循环中将数组插入数据库
				
        
 
            
        基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				