chaitu chaitu
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
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
What code do you have at the moment for inserting in the table (are you talking about a JTable or a database table?)
ASKER
normal DB insert statement
ASKER
String insert ="INSERT INTO EMP(EMPID,NAME,SAL) VALUES(?,?,?) ";
pstmt =con.prepareStatement(inse rt);
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
pstmt =con.prepareStatement(inse
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
ASKER
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
ASKER
i dont know how to write it to csv file
ASKER
for every column name adjacent to that its data should be prsent;
i am inserting into a big table.
i am inserting into a big table.
ASKER
if u take emp table
how will u insert data and column parallely
how will u insert data and column parallely
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
String insert ="INSERT INTO EMP(EMPID,NAME,SAL) VALUES(?,?,?) ";
pstmt =con.prepareStatement(inse rt);
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
pstmt =con.prepareStatement(inse
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");
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(",");
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
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
Writing to file: http://javaalmanac.com/egs/java.io/WriteToFile.html
ASKER
IS IT OK
File tokenFile = new File("c:\\token-file.csv") ;
pstmt =con.prepareStatement(inse rt);
pstmt.setString(1,"sss");
pstmt.setString(2,"aaa");
pstmt.setString(3,"dd");
int count = pstmt.executeUpdate();
System.out.println("Record s 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();
File tokenFile = new File("c:\\token-file.csv")
pstmt =con.prepareStatement(inse
pstmt.setString(1,"sss");
pstmt.setString(2,"aaa");
pstmt.setString(3,"dd");
int count = pstmt.executeUpdate();
System.out.println("Record
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)
ASKER
(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
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";
String col1Val = "sss";
Almost correct ;°)
You probably want:
printWriter.print("EMPID") ;
printWriter.print(","); // <<<<<< added
printWriter.print("NAME");
printWriter.print(","); // <<<<<< added
printWriter.print("SAL");
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");
printWriter.println("SAL")
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks
:-)