0

I am doing XML unmarshalling using java jaxb and converting them into strings and calling the function that stores the string in a sql table. Now I want to store all of string in a list and insert them in a sql table. Based on the length of list, I ll do them bulk insert I don't want to call the table insert function everytime.I need help on modifying the code accordingly

    XMLInputFactory xif = XMLInputFactory.newFactory();
    Reader reader = new StringReader(response.toString());
    XMLStreamReader xsr = xif.createXMLStreamReader(reader);
     while(xsr.hasNext()) {
        if(xsr.isStartElement() && xsr.getLocalName().equals("customer")) {
          JAXBContext jc = JAXBContext.newInstance(Customer.class);
          Unmarshaller unmarshaller = jc.createUnmarshaller();
          Customer jb = unmarshaller.unmarshal(xsr,Customer.class).getValue();
         String  customer_name=jb.getNAME();
         String customer_id=jb.getCUSTOMERID();
         String entity_id=jb.getENTITY();
         System.out.println("Customer Name" +customer_name);
         System.out.println("Customer ID" +customer_id);
         System.out.println("Entity ID" +entity_id);
         String insertStatement = "TABLEINSERT (customer_id, customer_name, entity_id)"; 
             insertStatements.add(insertStatement);
         }

          xsr.next();

     }

      if(insertStatements.size() > 10){
          TABLEINSERT (customer_id, customer_name, entity_id); 
        } else {
          System.out.println("Nothing");
        }


private static void TABLEINSERT (String CustomerID,String CustomerName, String EntityID)
{
  try
  {
    System.out.println("Inserting....");
    PreparedStatement statement = con.prepareStatement("INSERT INTO intacct_customer (customer_id,customer_name,entity_id) VALUES ( ?, ?,?)");
    statement.setString(1, CustomerID);
    statement.setString(2, CustomerName);
    statement.setString(3, EntityID);
    statement.execute();
  }
  catch (Exception e)
  {
    System.out.println(e.getMessage());
    System.exit(0);
  }
    }
Karthi
  • 708
  • 1
  • 19
  • 38
  • possible duplicate of [Java: Insert multiple rows into MySQL with PreparedStatement](http://stackoverflow.com/questions/4355046/java-insert-multiple-rows-into-mysql-with-preparedstatement) – WilQu Aug 14 '14 at 12:38

1 Answers1

0

You can create insert statements and store it in a list. Finally based on the size of the list you can choose to perform a insert in a loop or a bulk one.

XMLInputFactory xif = XMLInputFactory.newFactory();
Reader reader = new StringReader(response.toString());
XMLStreamReader xsr = xif.createXMLStreamReader(reader);
List<String> insertStatements = new ArrayList<>();
 while(xsr.hasNext()) {
    if(xsr.isStartElement() && xsr.getLocalName().equals("customer")) {
      JAXBContext jc = JAXBContext.newInstance(Customer.class);
      Unmarshaller unmarshaller = jc.createUnmarshaller();
      Customer jb = unmarshaller.unmarshal(xsr,Customer.class).getValue();
     String  customer_name=jb.getNAME();
     String customer_id=jb.getCUSTOMERID();
     String entity_id=jb.getENTITY();
     System.out.println("Customer Name" +customer_name);
     System.out.println("Customer ID" +customer_id);
     System.out.println("Entity ID" +entity_id);
     String insertStatement = INSERTTABLE(customer_id, customer_name, entity_id); -> I dont want to call them everytime. I need to store them into strings and i need to bulk based on the length of the list. 
     insertStatements.add(insertStatement);

    }

     xsr.next();

}

if(insertStatements.size > 10){
  // perform bulk insert
} else {
  // usual insert
}
Saket
  • 3,079
  • 3
  • 29
  • 48
  • I updated my code. Can you tell me if this will work. I am new to programming and hence finding these stuffs confusing. SUggest me if any changes need to me made – Karthi Aug 14 '14 at 12:37
  • I don't see the method called IntacctCustomer. Moreover the insert statement which you are creating is not correct. If you fix those problems it should work. Are you not compiling and running it? – Saket Aug 14 '14 at 12:41
  • I just corrected the names. that was a typo mistake. When I compile this program I get the following problems.customer_id cannot be resolved to a variable customer_name cannot be resolved to a variable entity_id cannot be resolved to a variable – Karthi Aug 14 '14 at 12:44
  • That's because you have those variables inside the loop and using it outside. I would suggest that you ready about [scopes](http://www.java-made-easy.com/variable-scope.html), passing values to methods and the link posted in the comments of the question by @wilqu – Saket Aug 14 '14 at 12:51