Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

inserting data into database simultaneously i want to write into a file(excel)

i am inserting into a table which has 200 columns.when i see the spy log i am confused that which column has data and which column has no data.

while inserting data into database simultaneously i want to write into a file(excel) in such a manner that it should dipplay columns name adjacent to that its data.

column name    data

column1             1
column2             'fffff'
column3              'ddd'
column4             34
.      
.
.
etc
Avatar of zzynx
zzynx
Flag of Belgium image

What code do you have at the moment for inserting in the table (are you talking about a JTable or a database table?)
Avatar of chaitu chaitu

ASKER

normal DB insert statement
String insert ="INSERT INTO EMP(EMPID,NAME,SAL) VALUES(?,?,?) ";


 pstmt =con.prepareStatement(insert);
             pstmt.setString(1,"sss");
             pstmt.setString(2,"aaa");
             pstmt.setString(3,"dd");
             int count = pstmt.executeUpdate();
         

        SAY I AM INSERTING DATA INTO EMP TABLE

      parrallely  i want to store in a file  column wise data
preferably excel sheet
Just write it as a csv file then you can open it in Excel
You wouldn't want to have the column names adjacent every time as that would be quite redundant, unless you've a very good reason
i dont know how to write it to csv file
for every column name adjacent to that its data should be prsent;


i am inserting into a big table.
if u take emp table

how will u insert data and column parallely
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you do it  like that it will open as a table in Excel.
You could try writing it with the column names adjacent, but i don't know if it'd open properly in Excel. For one thing, it'd think it had nio column names
String insert ="INSERT INTO EMP(EMPID,NAME,SAL) VALUES(?,?,?) ";


 pstmt =con.prepareStatement(insert);
             pstmt.setString(1,"sss");
             pstmt.setString(2,"aaa");
             pstmt.setString(3,"dd");
             int count = pstmt.executeUpdate();


           for the above code how will u write in a csv file in the following format:

Column1Name, Column2Name, Column3Name
col1row1val, col2row1val,col3row1val
(Having written the column headers already)

printWriter.print("sss");
printWriter.print("aaa");
printWriter.println("dd");
Oops

printWriter.print("sss");
printWriter.print(",");
printWriter.print("aaa");
printWriter.print(",");
printWriter.print("dd");
printWriter.println(",");
>> i dont know how to write it to csv file
If you save a file with .csv as extension and this as content:

Column1,1
Column2,2
Column3,3
Column4,4
Column1,10
Column2,20
Column3,30
Column4,40

Double clicking it will make Excel open it.
And you'll have the column names in the first column and the corr. value in the 2nd column
IS IT OK

 File tokenFile = new File("c:\\token-file.csv");
       
       
            pstmt =con.prepareStatement(insert);
             pstmt.setString(1,"sss");
             pstmt.setString(2,"aaa");
             pstmt.setString(3,"dd");
             int count = pstmt.executeUpdate();
            System.out.println("Records Updated "+count);
           
            con.commit();
           
            PrintWriter printWriter = new PrintWriter(new FileWriter(tokenFile, true));
           
            printWriter.print("EMPID");
            printWriter.print("NAME");
            printWriter.print("SAL");
           
            printWriter.print("sss");
            printWriter.print(",");
            printWriter.print("aaa");
            printWriter.print(",");
            printWriter.print("dd");
            printWriter.println(",");
             printWriter.close();
Fine
(Although you'd probably hold the values in variables, rather than write literals. This would be more efficient and less error prone)
(Although you'd probably hold the values in variables, rather than write literals. This would be more efficient and less error prone)


ARE U TALKING ABT THIS

printWriter.print("sss");

for example i am passing like this
Yes. You're writing the same value twice so hold it in a variable

String col1Val = "sss";
Almost correct ;°)
You probably want:

            printWriter.print("EMPID");
            printWriter.print(","); // <<<<<< added
            printWriter.print("NAME");
            printWriter.print(","); // <<<<<< added
            printWriter.print("SAL");
and

    printWriter.println("SAL");  // <<<<<< println instead of print

and

    printWriter.print("dd");
    printWriter.println(",");

can be replaced by

    printWriter.println("dd");
And of course, you have to write the headers once and the fields in the db insert loop
Yes - missed that about the column headers - they're the same as the rest
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks
:-)