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.
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:
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.