|
1 | | -dbtableprinter |
2 | | -============== |
| 1 | +Database Table Printer |
| 2 | +====================== |
3 | 3 |
|
4 | | -Database Table Printer - a Java utility class to print a pretty table to standard out. |
| 4 | +**DBTablePrinter** is a Java utility class for printing rows from a given database table or a `java.sql.ResultSet` to standard out, formatted to look like a table with rows and columns with borders. It is part of my efforts to learn Java, Javadoc, how to use an IDE (IntelliJ IDEA 13.1.15 in this case), how to apply an open source license and how to use Git and GitHub for version control and publishing an open source software. |
| 5 | + |
| 6 | +## Basic Usage |
| 7 | + |
| 8 | +There are two overloaded static methods: `printTable()` and `printResultSet()`. Here is how to use them: |
| 9 | + |
| 10 | +```java |
| 11 | +// Create a connection to the database |
| 12 | +Connection conn = DriverManager.getConnection(url, username, password); |
| 13 | + |
| 14 | +// Just pass the connection and the table name to printTable() |
| 15 | +DBTablePrinter.printTable(conn, "employees"); |
| 16 | +``` |
| 17 | + |
| 18 | +It should print something like this: |
| 19 | + |
| 20 | +``` |
| 21 | +Printing 10 rows from table(s) EMPLOYEES |
| 22 | ++--------+------------+------------+-----------+--------+-------------+ |
| 23 | +| EMP_NO | BIRTH_DATE | FIRST_NAME | LAST_NAME | GENDER | HIRE_DATE | |
| 24 | ++--------+------------+------------+-----------+--------+-------------+ |
| 25 | +| 10001 | 1953-09-02 | Georgi | Facello | M | 1986-06-26 | |
| 26 | ++--------+------------+------------+-----------+--------+-------------+ |
| 27 | +| 10002 | 1964-06-02 | Bezalel | Simmel | F | 1985-11-21 | |
| 28 | ++--------+------------+------------+-----------+--------+-------------+ |
| 29 | + . |
| 30 | + . |
| 31 | +``` |
| 32 | + |
| 33 | +`printTable()` uses `DEFAULT_MAX_ROWS`, which is 10, to limit the number of rows to query and print. This can be changed by calling the method with a third parameter: |
| 34 | + |
| 35 | +```java |
| 36 | +int maxRows = 3; |
| 37 | +DBTablePrinter.printTable(conn, "employees", maxRows); |
| 38 | +``` |
| 39 | + |
| 40 | +`printResultSet()` on the other hand, does not have this limit and prints each row of the given `ResultSet`: |
| 41 | + |
| 42 | +```java |
| 43 | +Statement stmt = conn.createStatement(); |
| 44 | +ResultSet rs = stmt.executeQuery("SELECT * FROM EMPLOYEES LIMIT 15"); |
| 45 | +DBTablePrinter.printResultSet(rs); |
| 46 | +``` |
| 47 | + |
| 48 | +Joined tables and column labels can also be used with `printResultSet()`: |
| 49 | + |
| 50 | +```java |
| 51 | +String sql = "SELECT EMPLOYEES.EMP_NO AS ID, FIRST_NAME, LAST_NAME, SALARY, " + |
| 52 | + "FROM_DATE, TO_DATE FROM EMPLOYEES LEFT JOIN SALARIES " + |
| 53 | + "ON EMPLOYEES.EMP_NO = SALARIES.EMP_NO LIMIT 3"; |
| 54 | +ResultSet rs = stmt.executeQuery(sql); |
| 55 | +DBTablePrinter.printResultSet(rs); |
| 56 | +``` |
| 57 | + |
| 58 | +Should print: |
| 59 | + |
| 60 | +``` |
| 61 | +Printing 3 rows from table(s) EMPLOYEES, SALARIES |
| 62 | ++--------+------------+-----------+--------+-------------+-------------+ |
| 63 | +| ID | FIRST_NAME | LAST_NAME | SALARY | FROM_DATE | TO_DATE | |
| 64 | ++--------+------------+-----------+--------+-------------+-------------+ |
| 65 | +| 10001 | Georgi | Facello | 60117 | 1986-06-26 | 1987-06-26 | |
| 66 | ++--------+------------+-----------+--------+-------------+-------------+ |
| 67 | +| 10001 | Georgi | Facello | 62102 | 1987-06-26 | 1988-06-25 | |
| 68 | ++--------+------------+-----------+--------+-------------+-------------+ |
| 69 | +| 10001 | Georgi | Facello | 66074 | 1988-06-25 | 1989-06-25 | |
| 70 | ++--------+------------+-----------+--------+-------------+-------------+ |
| 71 | +``` |
| 72 | +The souce code is heavily commented with Javadoc and inline comments mainly for me to learn how to use them and maybe for a remote possibility that someone finds it useful. |
| 73 | + |
| 74 | +By the way, I used Java 8 during development but I didn't use lamdas, streams etc. so JREs > 1.5 should work (not tested though). I am planning to learn and use those but maybe not for this simple project ;) |
| 75 | + |
| 76 | +## License |
| 77 | +Database Table Printer |
| 78 | +Copyright (C) 2014 Hami Galip Torun |
| 79 | + |
| 80 | + |
| 81 | +Project Home: https://github.com/htorun/dbtableprinter |
| 82 | + |
| 83 | +This program is free software: you can redistribute it and/or modify |
| 84 | +it under the terms of the GNU General Public License as published by |
| 85 | +the Free Software Foundation, either version 3 of the License, or |
| 86 | +(at your option) any later version. |
| 87 | + |
| 88 | +This program is distributed in the hope that it will be useful, |
| 89 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 90 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 91 | +GNU General Public License for more details. |
| 92 | + |
| 93 | +You should have received a copy of the GNU General Public License |
| 94 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 commit comments