java打印表格 将ResultSet中的数据打印成表格
问题描述
MySQL
的查询语句输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| mysql> select * from instructor; +-------+------------+------------+-----------+ | ID | name | dept_name | salary | +-------+------------+------------+-----------+ | 10101 | Srinivasan | Comp. Sci. | 65000.00 | | 12121 | Wu | Finance | 90000.00 | | 15151 | Mozart | Music | 40000.00 | | 22222 | Einstein | Physics | 95000.00 | | 25566 | Brown | Biology | 100000.00 | | 30765 | Green | Music | NULL | | 32343 | El Said | History | 60000.00 | | 33456 | Gold | Physics | 87000.00 | | 45565 | Katz | Comp. Sci. | 75000.00 | | 58583 | Califieri | History | 62000.00 | | 76543 | Singh | Finance | 80000.00 | | 76766 | Crick | Biology | 72000.00 | | 77987 | Kim | Physics | 98000.00 | | 83821 | Brandt | Comp. Sci. | 92000.00 | | 88877 | Perry | Finance | 125000.00 | | 88878 | Perry | Finance | 125000.00 | | 98345 | Kim | Elec. Eng. | 80000.00 | +-------+------------+------------+-----------+ 17 rows in set (0.04 sec)
|
使用JDBC
执行上述的查询语句,将结果集中的查询结果以表格的形式打印出来。
思路
- 通过结果集的元数据可以知道结果集中的列数,和列名.
- 然后遍历结果集,分别统计每一列中的最大字符数。
- 然后通过
System.out.printf()
方法进行格式化输出。
字符串格式化方式
左对齐
%-10s
表示这个字符串的长度为10,不足10的地方以空格填充,带-
的表示左对齐.
1 2
| System.out.printf("属性:%-10s ", rsmd.getColumnName(i)); System.out.printf("类型:%-10s\n", rsmd.getColumnTypeName(i));
|
1 2 3 4
| 属性:ID 类型:VARCHAR 属性:name 类型:VARCHAR 属性:dept_name 类型:VARCHAR 属性:salary 类型:DECIMAL
|
右对齐
%10s
表示这个字符串的长度为10,不足10的地方以空格填充,默认右对齐
1 2
| System.out.printf("属性:%10s ", rsmd.getColumnName(i)); System.out.printf("类型:%10s\n", rsmd.getColumnTypeName(i));
|
1 2 3 4
| 属性: ID 类型: VARCHAR 属性: name 类型: VARCHAR 属性: dept_name 类型: VARCHAR 属性: salary 类型: DECIMAL
|
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package tools;
import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator;
public class ResultSetPrinter { public static void printResultSet(ResultSet rs) throws SQLException { ResultSetMetaData resultSetMetaData = rs.getMetaData(); int ColumnCount = resultSetMetaData.getColumnCount(); int[] columnMaxLengths = new int[ColumnCount]; ArrayList<String[]> results = new ArrayList<>(); while (rs.next()) { String[] columnStr = new String[ColumnCount]; for (int i = 0; i < ColumnCount; i++) { columnStr[i] = rs.getString(i + 1); columnMaxLengths[i] = Math.max(columnMaxLengths[i], (columnStr[i] == null) ? 0 : columnStr[i].length()); } results.add(columnStr); } printSeparator(columnMaxLengths); printColumnName(resultSetMetaData, columnMaxLengths); printSeparator(columnMaxLengths); Iterator<String[]> iterator = results.iterator(); String[] columnStr; while (iterator.hasNext()) { columnStr = iterator.next(); for (int i = 0; i < ColumnCount; i++) { System.out.printf("|%" + columnMaxLengths[i] + "s", columnStr[i]); } System.out.println("|"); } printSeparator(columnMaxLengths); }
private static void printColumnName(ResultSetMetaData resultSetMetaData, int[] columnMaxLengths) throws SQLException { int columnCount = resultSetMetaData.getColumnCount(); for (int i = 0; i < columnCount; i++) { System.out.printf("|%" + columnMaxLengths[i] + "s", resultSetMetaData.getColumnName(i + 1)); } System.out.println("|"); }
private static void printSeparator(int[] columnMaxLengths) { for (int i = 0; i < columnMaxLengths.length; i++) { System.out.print("+"); for (int j = 0; j < columnMaxLengths[i]; j++) { System.out.print("-"); } } System.out.println("+"); }
}
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| package tools; import java.sql.*;
public class Test { private static String driver = "com.mysql.cj.jdbc.Driver"; private static String URL = "jdbc:mysql://127.0.0.1:3306/university?serverTimezone=UTC"; private static String user = "root"; private static String password = "root";
private static void JDBCexample() { try {
Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } testStatement(); }
private static void testStatement() { try ( Connection conn = DriverManager.getConnection(URL, user, password); Statement stmt = conn.createStatement()) { testSqlInjecton(stmt);
} catch (SQLException sqle) { System.out.println("SQLException : " + sqle); } }
private static void testSqlInjecton(Statement stmt) throws SQLException { String name = "X' or 'Y' = 'Y"; String sql = "select * from instructor where name = '" + name + "'"; ResultSet rs = stmt.executeQuery(sql); ResultSetPrinter.printResultSet(rs); }
public static void main(String[] args) { JDBCexample(); } }
|
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| +-----+----------+----------+---------+ | ID| name| dept_name| salary| +-----+----------+----------+---------+ |10101|Srinivasan|Comp. Sci.| 65000.00| |12121| Wu| Finance| 90000.00| |15151| Mozart| Music| 40000.00| |22222| Einstein| Physics| 95000.00| |25566| Brown| Biology|100000.00| |30765| Green| Music| null| |32343| El Said| History| 60000.00| |33456| Gold| Physics| 87000.00| |45565| Katz|Comp. Sci.| 75000.00| |58583| Califieri| History| 62000.00| |76543| Singh| Finance| 80000.00| |76766| Crick| Biology| 72000.00| |77987| Kim| Physics| 98000.00| |83821| Brandt|Comp. Sci.| 92000.00| |88877| Perry| Finance|125000.00| |88878| Perry| Finance|125000.00| |98345| Kim|Elec. Eng.| 80000.00| +-----+----------+----------+---------+
|