开发者

Easy way to dump a table's contents to a ASCII formatted table string?

开发者 https://www.devze.com 2023-04-12 04:00 出处:网络
I am debuging some database codeon the Java Platform and it would be convenient to dump the contents of a database table at various points to a log file.

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 ta开发者_运维技巧ke 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号