2

I am debuging some database code on the Java Platform and it would be convenient to dump the contents of a database table at various points to a log file.

Is there a java library which will take a JDBC connection and a table name and dump all of the contents to a string (ideally formatted into columns properly)?

output should look something like:

| col1 | col2 |
| 22   | 55   | 
| 23   | 99   | 

I know I could write this my self but it feels like the kind of thing that may already be out there. However after lots of searching I have not seen anything.

Would also be interested in any java library for just formatting the ASCI table as I may have to solve the problem my self.

Mark
  • 467
  • 3
  • 14

2 Answers2

1

You don't need Java. Check if your db has something like the following command (which works for mysql):

SELECT * INTO OUTFILE '/my_table.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM my_table; 

EDIT: If you open the file with a spreadsheet reader like excel, the data will be lined up. Otherwise, not. In order to line up exactly the columns of an ASCII file, the only way that I know is to check the length of each field and add spaces when necessary.

I don't know Java libraries doing this stuff. But it should not particularly difficult to implement. Only a bit tedious...

perissf
  • 15,979
  • 14
  • 80
  • 117
  • will this give me nice lined up columns? That might work for me in some cases but I have 2 databases on the go hear (Orical and Derby) so would probably have to do the db specific solution twice... Im still holding out for a JDBC/java only solution. – Mark Oct 18 '11 at 16:58
0

DbUnit has methods to import and export database tables to and from XML form. You will have to add the code to write it into a file yourself.

rsp
  • 23,135
  • 6
  • 55
  • 69
  • Thanks but I dont wont xml. I wont a asci table of data for humans to read. – Mark Oct 10 '11 at 14:19
  • You *won't* or you *don't want to*? –  Oct 10 '11 at 14:32
  • The text is to be used by humans not computers so the format shown in my (updated) question would be most convent. The code to dump it to a log file is no problem. – Mark Oct 10 '11 at 14:44