在oracle数据库中存储arraylist

时间:2015-07-11 06:40:15

标签: java arraylist oracle10g

这是我在数据库中存储arraylist的代码,但我面临的一些问题只是arraylist的第一个元素存储在数据库中。 请帮忙

  int invoice_no,bookno;
  static ArrayList<Integer> reference=new ArrayList<>();
  static ArrayList<Integer> quantity2 = new ArrayList<>();


  public void abc1()
  {
       try
    {
        System.out.println("hello");
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","anchit");
        Statement statement=connection.createStatement();

       ResultSet r1=statement.executeQuery("select max(invoice_no) from invoice_detail");
         int a=1;

       while(r1.next())
        {
             a=r1.getInt(1);
            System.out.println(r1.getInt(1));
        }

        System.out.println("a="+a);
        invoice_no=a+1;
        if(invoice_no%50==0)
            bookno=(invoice_no/50);
        else
            bookno=(invoice_no/50)+1;

        System.out.println(invoice_no+","+bookno);
        System.out.println(reference);

        for(int i=0;i<reference.size();i++)
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection connection1=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","anc123hit");
        Statement statement1=connection1.createStatement();

            //int b=
            System.out.println(reference.get(i));
            //int c=quantity2.get(i);
             ResultSet r=statement1.executeQuery("insert into invoice_detail values("+invoice_no+","+bookno+","
                    +reference.get(i)+","+quantity2.get(i)+", to_char(sysdate,'dd-mon-yy'),'anchit','abcd')");
             r.next();
            connection1.close();
        }
        connection.close();
    }
    catch(Exception e)
    {}
  }

这是我在数据库中存储arraylist的代码,但我遇到了一些问题 只有arraylist的第一个元素存储在数据库中。 请帮忙

1 个答案:

答案 0 :(得分:0)

当您尝试在DML(.executeQuery()insertupdate)操作上使用delete时会出现此问题,您应该使用.executeUpdate()。< / p>

 Statement statement1=connection1.createStatement();

            //int b=
    System.out.println(reference.get(i));
            //int c=quantity2.get(i);
             //ResultSet r=;
    statement1.executeUpdate("insert into invoice_detail values("+invoice_no+","+bookno+","
                    +reference.get(i)+","+quantity2.get(i)+", to_char(sysdate,'dd-mon-yy'),'anchit','abcd')");
             //r.next();

通常编译器抛出错误,当您在DML上使用.executeQuery时,查询不返回ResultSet