简体   繁体   中英

store arraylist in oracle database

this is my code for storing arraylist in database but i am facing some problem that is only the first element of arraylist is stored in database. pls help

  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)
    {}
  }

this is my code for storing arraylist in database but i facing some problem only the first element of arraylist is stored in database. pls help

This behavior comes when you try to use .executeQuery() on DML ( insert , update , delete ) operation, you should use .executeUpdate() instead.

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

Usually compiler thrown error that your query doesn't return ResultSet when you use .executeQuery on DML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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