1

i am trying to insert data from excel sheet into database in java. for that i have connected both the database using ODBC. excelsheet has 2 columns cname and title. after querying the Excel spreadsheet i stored the resultSet values of cname and title into arraylist.

      List Cname = new  ArrayList();
      List Title=new ArrayList();

            Cname.add(rs.getString("Cname"));
            Title.add(rs.getString("Title"));

this will result like this

      [aaa, bbb,cccc, dddd]
      [tree,human,animal,bird]

when i try to insert this into SQL database using insert query it is getting stored like this.

    statement1.executeUpdate("INSERT INTO dbo.company (Cname,Title)  SELECT      
   '"+Cname+"','"+Title+"'");


       Cname                               Title

    [aaa, bbb,cccc, dddd]        [tree,human,animal,bird]

but i want to store this as

      Cname               Title
     ___________________________
      aaa                  tree
      bbb                 human
      cccc                animal
      ddd                  bird

how do i do this???pls help to solve in this.

4 Answers 4

1

You'll have to insert/update the list values, actually you're inserting the string representations of the entire lists..

Assuming, both lists do exist (not null) and have the same length, then this is a trivial solution:

for (int i = 0; i < Cname.size(); i++ {
   statement1.executeUpdate("INSERT INTO dbo.company (Cname,Title)  SELECT      
     '"+Cname.get(i)+"','"+Title.get(i)+"'");
}

Note - every java class has an implementation of the toString() method, and that method is called, when you "use an object as a string", like in the expression to create the SQL statement. For lists, the method returns a String that simply includes the (String-representations of) the list element in brackets.

Sign up to request clarification or add additional context in comments.

Comments

1

Put a for loop around your insert statement:

for(int i = 0; i < Cname.size(); i++)
    statement1.executeUpdate("INSERT INTO dbo.company (Cname,Title) values ( '"+Cname.get(i)+"','"+Title.get(i)+"')");

Comments

0

If your db is SQL Server, you can reference the followng Q & A. Just prepare xml data and pass to Stored Procedure.

How to Update SQL Server Table With Data From Other Source (DataTable)

Comments

0

You can create a single SQL statement of this form:

INSERT INTO dbo.company (Cname, Title) 
VALUES ('aaa', 'tree'), 
       ('bbb', 'human'),
       ('cccc', 'animal'),
       ('dddd', 'bird')

With JDBC:

StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO dbo.company (Cname,Title) VALUES ");

String glue = "";
for (int i = 0; i < Cname.size(); i++) {
  sql.append(glue);
  sql.append("('");
  sql.append(Cname.get(i).toString().replace("'", "''"));
  sql.append("', '");
  sql.append(Title.get(i).toString().replace("'", "''"));
  sql.append("')");
  glue = ", ";
}

statement1.executeUpdate(sql.toString());

An alternative syntax (if the above doesn't work for Excel spreadsheets) is this:

INSERT INTO dbo.company (Cname, Title) 
SELECT 'aaa', 'tree' UNION ALL 
SELECT 'bbb', 'human' UNION ALL
SELECT 'cccc', 'animal' UNION ALL
SELECT 'dddd', 'bird'

Or with JDBC

StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO dbo.company (Cname,Title) ");

String glue = "";
for (int i = 0; i < Cname.size(); i++) {
  sql.append(glue);
  sql.append("SELECT '");
  sql.append(Cname.get(i).toString().replace("'", "''"));
  sql.append("', '");
  sql.append(Title.get(i).toString().replace("'", "''"));
  sql.append("'");
  glue = " UNION ALL ";
}

statement1.executeUpdate(sql.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.