繁体   English   中英

使用JAVA在HANA中插入数组

[英]Insert array in HANA with JAVA

我有一个对象的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阵列。 任何帮助表示赞赏。

@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      

这不是一个标准的代码,但仍然可以正常工作。希望它可以帮助任何人。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM