使用串联和Varray将值插入表中

时间:2015-10-30 06:18:56

标签: java sql jdbc

这是我的代码,其中METHODARGDATATYPE_VARRAY是我表中第5列的varray,第一列是自动生成的序列与't'连接。

 String arrayElements[] = { "Test3", "Test4" };
   ArrayDescriptor desc = ArrayDescriptor.createDescriptor
                                        ("METHODARGDATATYPE_VARRAY", conn);
   ARRAY newArray = new ARRAY(desc, conn, arrayElements);

   String sql="insert into TestCaseIDDetails values (concat('t',TestCaseID_sequence.nextval),?,?,?,?)";
   PreparedStatement ps =
     conn.prepareStatement (sql);
   ps.setString(2,testCaseIDandDetailsBean.getClass_name()) ;
   ps.setString(3,testCaseIDandDetailsBean.getMethod_name()) ;
   ps.setString(4,testCaseIDandDetailsBean.getMethodReplacement()) ;
   ((OraclePreparedStatement)ps).setARRAY (5, newArray);

   ps.execute ();

我试图执行此代码,但我再次遇到错误如下:

      java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OraclePreparedStatement.setARRAYInternal(OraclePreparedStatement.java:5906)
at oracle.jdbc.driver.OraclePreparedStatement.setARRAY(OraclePreparedStatement.java:5898)
at implementation.TestCaseIDandDetailsDAOImpl.addTestCaseIDandDetails(TestCaseIDandDetailsDAOImpl.java:54)
at implementation.TestCaseIDandDetailsDAOImpl.main(TestCaseIDandDetailsDAOImpl.java:134)

我的桌子是:

     CREATE or replace TYPE METHODARGDATATYPE_VARRAY AS VARRAY(20) OF varchar2(30);
     create table TestCaseIDDetails(
       testcaseID varchar2(20) primary key,
       classname varchar2(20) not null,
       methodname varchar2(20) not null,
       MethodReplacement char(2) check(MethodReplacement in ('y','n')),
       MethodArgDataType METHODARGDATATYPE_VARRAY);

     Create sequence TestCaseID_sequence minvalue 1 start with 1 increment by 1 ;

2 个答案:

答案 0 :(得分:3)

索引应该对应于预准备语句中的问号占位符的索引,到表中的列号。第一个问号的索引是1,第二个是2,依此类推。你的索引都是一个,应该是1 2 3 4而不是2 3 4 5。

答案 1 :(得分:1)

更改以下内容

ps.setString(2,testCaseIDandDetailsBean.getClass_name()) ;
   ps.setString(3,testCaseIDandDetailsBean.getMethod_name()) ;
   ps.setString(4,testCaseIDandDetailsBean.getMethodReplacement()) ;
   ((OraclePreparedStatement)ps).setARRAY (5, newArray);

ps.setString(1,testCaseIDandDetailsBean.getClass_name()) ;
   ps.setString(2,testCaseIDandDetailsBean.getMethod_name()) ;
   ps.setString(3,testCaseIDandDetailsBean.getMethodReplacement()) ;
   ((OraclePreparedStatement)ps).setARRAY (4, newArray);