如何使用JDBC插入在表中插入数组

时间:2015-07-23 06:09:23

标签: java arrays jdbc

我正在尝试使用jdbc驱动程序将值插入表中。在我的表中,一列被定义为数组数据类型。

表格如下

    CREATE TABLE userType
    (
      id bigserial NOT NULL, // auto Inc
      type character varying,
      userRole bigint[] // array
    )

我的代码中有一个数组,它是从arraylist转换而来的。

    List<Long> ids = new ArrayList<Long>();
    ids.add("1");
    ids.add("2");
    ids.add("3");
    ids.add("4");

    Long[] idArr = new Long[ids.size()];
    idArr = ids.toArray(idArr);

我使用以下代码将数据插入表中。

    String querys = "insert into userType(type,fk_last_modified_by,userRole)"
                + " values ('Auto',1,"+ idArr+")";
        Connection connections = sessionFactory.getCurrentSession().connection();
        Statement stmts = connections.createStatement();
        int count =stmts.executeUpdate(querys);
        System.out.println("count---"+count);
        connections.close();

执行上述操作时出现以下错误。

    org.postgresql.util.PSQLException: ERROR: syntax error at or near "["
      Position: 99
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:302)
        at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:196)
        at com.mmf.controllers.UpdateReconcileController.save(com.mmf.controllers.UpdateReconcileController:123)

然后我只是按照Jagdesh提供的解决方案,

        String querys = "insert into userType(type,fk_last_modified_by,userRole)"
                + " values (?,?,?)";
        System.out.println(querys);
        Connection connections = sessionFactory.getCurrentSession().connection();
        CallableStatement stmts = connections.prepareCall(query);
        stmts.setString(1, "Auto");
        stmts.setInt(2, 1);
        stmts.setArray(3, connections.createArrayOf("integer", idArr));
        stmts.executeUpdate(querys);;
        connections.close();

现在我收到以下错误,

    org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
        at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:53)
        at org.postgresql.core.v3.SimpleParameterList.setLiteralParameter(SimpleParameterList.java:114)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.bindLiteral(AbstractJdbc2Statement.java:2172)
        at org.postgresql.jdbc2.AbstractJdbc2Statement.setLong(AbstractJdbc2Statement.java:1227)
        at org.apache.commons.dbcp.DelegatingCallableStatement.setLong(DelegatingCallableStatement.java:252)

有人能指出我在哪里做错了吗?

3 个答案:

答案 0 :(得分:1)

而不是上面使用PreparedStatement

String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
                + " values (?,?,?)";

 Connection connections = sessionFactory.getCurrentSession().connection();
        PreparedStatement pstmts = connections.createStatement();
    pstmts.SetString("Auto");
    pstmts.SetInt(1);
    pstmts.setArray(3, conn.createArrayOf("integer", idArr));
        pstmts.executeUpdate(querys);

答案 1 :(得分:0)

  

插入reconcile_process(process_type,fk_last_modified_by,fk_bank_stmt_id)值(&#39; Auto&#39;,&#39; [Ljava.lang.Long; @ b318fc5&#39;)

所以你不能只用+字符串的长类型。

答案 2 :(得分:0)

我认为你想使用PreparedStatement,而不是CallableStatement。 CallableStatements用于调用SQL函数,它有一种奇怪的语法。