• poi+ResultSet+线程池导出数据库表结构


    1. public class MainTest {
    2. private static ThreadPoolExecutor pool = new ThreadPoolExecutor(
    3. 5,
    4. 20,
    5. 8,
    6. TimeUnit.SECONDS,
    7. new ArrayBlockingQueue<>(5),
    8. Executors.defaultThreadFactory(),
    9. new ThreadPoolExecutor.AbortPolicy()
    10. );
    11. public static void main(String[] args) {
    12. String extSchema = "";
    13. String driver = "com.mysql.cj.jdbc.Driver";
    14. //为了保密url暂时不写了
    15. String url = "";
    16. String user = "";
    17. String password = "";
    18. printTableStructure(driver, url, user, password,extSchema);
    19. }
    20. /**
    21. * 打印所有表结构
    22. *
    23. * @param driver driver
    24. * @param url url
    25. * @param user user
    26. * @param password password
    27. * @throws Exception exception
    28. */
    29. private static void printTableStructure(String driver, String url, String user, String password,String extSchema){
    30. try{
    31. Class.forName(driver);
    32. Connection connection = DriverManager.getConnection(url, user, password);
    33. DatabaseMetaData metaData = connection.getMetaData();
    34. ResultSet schemaResultSet = metaData.getCatalogs();
    35. //同步单个库
    36. if(!StringUtils.isEmpty(extSchema)){
    37. generateFile(connection,metaData,extSchema);
    38. }else{
    39. Long start = System.currentTimeMillis();
    40. //CountDownLatch latch = new CountDownLatch(18);
    41. //同步所有库
    42. while(schemaResultSet.next()){
    43. String schema = schemaResultSet.getString("TABLE_CAT");
    44. if("information_schema".equals(schema) ||
    45. "seata".equals(schema) ||
    46. "szcgc".equals(schema) ||
    47. "szcgc_project".equals(schema)){
    48. continue;
    49. }
    50. //pool.execute(() -> {
    51. generateFile(connection,metaData,schema);
    52. //latch.countDown();
    53. //});
    54. }
    55. //pool.shutdown();
    56. /*try{
    57. latch.await();
    58. }catch (InterruptedException exception){
    59. exception.getMessage();
    60. }*/
    61. System.out.println("同步耗时: "+ (System.currentTimeMillis() - start));
    62. }
    63. } catch (ClassNotFoundException e) {
    64. e.printStackTrace();
    65. } catch (SQLException throwables) {
    66. throwables.printStackTrace();
    67. }
    68. }
    69. private static void generateFile(Connection connection,DatabaseMetaData metaData,String schema){
    70. try{
    71. // 获取所有表
    72. ResultSet tableResultSet = metaData.getTables(schema, schema, "%", new String[]{"TABLE"});
    73. //poi生成excel
    74. XSSFWorkbook workbook = new XSSFWorkbook();
    75. //生成sheet
    76. XSSFSheet sheetIndex = workbook.createSheet("index");
    77. //设置列宽
    78. setColumnWidth(sheetIndex,1);
    79. //使用线程安全的类
    80. AtomicInteger i = new AtomicInteger(0);
    81. while (tableResultSet.next()) {
    82. try {
    83. String tableName= tableResultSet.getString("TABLE_NAME");
    84. String tableRemark = tableResultSet.getString("REMARKS");
    85. Row row0 = sheetIndex.createRow(i.get());
    86. Cell cell0 = row0.createCell(0);
    87. cell0.setCellValue(tableRemark);
    88. cell0.setCellStyle(indexCellStyle(workbook,"1"));
    89. Cell cell1 = row0.createCell(1);
    90. cell1.setCellValue(tableName);
    91. cell1.setCellStyle(indexCellStyle(workbook,"2"));
    92. //单元格加超链接
    93. CreationHelper createHelper = workbook.getCreationHelper();
    94. XSSFHyperlink hyperlink = (XSSFHyperlink) createHelper.createHyperlink(HyperlinkType.DOCUMENT);
    95. hyperlink.setAddress("#"+tableName+"!A1");
    96. cell1.setHyperlink(hyperlink);
    97. i.incrementAndGet();
    98. //生成表sheet
    99. XSSFSheet sheetTable = workbook.createSheet(tableName);
    100. setDetailColumnWidth(sheetTable);
    101. Row rowTable0 = sheetTable.createRow(0);
    102. rowTable0.setHeight((short)400);
    103. Cell cellTable00 = rowTable0.createCell(0);
    104. cellTable00.setCellValue(tableName);
    105. Cell cellTable01 = rowTable0.createCell(1);
    106. cellTable01.setCellValue(tableRemark);
    107. Row rowTable1 = sheetTable.createRow(1);
    108. rowTable1.setHeight((short)600);
    109. Cell cellTable10 = rowTable1.createCell(0);
    110. cellTable10.setCellValue("键说明");
    111. cellTable10.setCellStyle(detailCellStyle(workbook));
    112. Cell cellTable11 = rowTable1.createCell(1);
    113. cellTable11.setCellValue("字段名称");
    114. cellTable11.setCellStyle(detailCellStyle(workbook));
    115. Cell cellTable12 = rowTable1.createCell(2);
    116. cellTable12.setCellValue("字段类型");
    117. cellTable12.setCellStyle(detailCellStyle(workbook));
    118. Cell cellTable13 = rowTable1.createCell(3);
    119. cellTable13.setCellValue("默认值");
    120. cellTable13.setCellStyle(detailCellStyle(workbook));
    121. Cell cellTable14 = rowTable1.createCell(4);
    122. cellTable14.setCellValue("空值");
    123. cellTable14.setCellStyle(detailCellStyle(workbook));
    124. Cell cellTable15 = rowTable1.createCell(5);
    125. cellTable15.setCellValue("字段描述");
    126. cellTable15.setCellStyle(detailCellStyle(workbook,"1"));
    127. //获取表所有主键
    128. ResultSet pkColumns = connection.getMetaData().getPrimaryKeys(null,null,tableName);
    129. // 获取表字段结构
    130. ResultSet columnResultSet = metaData.getColumns(schema, schema, tableName, "%");
    131. int j = 2;
    132. while (columnResultSet.next()) {
    133. // 字段名称
    134. String columnName = columnResultSet.getString("COLUMN_NAME");
    135. // 数据类型
    136. String columnType = columnResultSet.getString("TYPE_NAME");
    137. // 字段长度
    138. int datasize = columnResultSet.getInt("COLUMN_SIZE");
    139. // 小数部分位数
    140. int digits = columnResultSet.getInt("DECIMAL_DIGITS");
    141. // 是否可为空 1代表可空 0代表不可为空
    142. int nullable = columnResultSet.getInt("NULLABLE");
    143. // 描述
    144. String remarks = columnResultSet.getString("REMARKS");
    145. //默认值
    146. String defValue = columnResultSet.getString("COLUMN_DEF");
    147. // 主键
    148. String pK = "NO";
    149. while(pkColumns.next()){
    150. String pkColumnName = pkColumns.getString("COLUMN_NAME");
    151. if(columnName.equals(pkColumnName)){
    152. pK = "PK";
    153. break;
    154. }
    155. }
    156. Row rowTable = sheetTable.createRow(j);
    157. Cell cellTable0 = rowTable.createCell(0);
    158. cellTable0.setCellValue(pK);
    159. Cell cellTable1 = rowTable.createCell(1);
    160. cellTable1.setCellValue(columnName);
    161. Cell cellTable2 = rowTable.createCell(2);
    162. cellTable2.setCellValue(columnType+"("+datasize+")");
    163. Cell cellTable3 = rowTable.createCell(3);
    164. cellTable3.setCellValue(defValue);
    165. Cell cellTable4 = rowTable.createCell(4);
    166. cellTable4.setCellValue(nullable == 0 ? "NOT NULL":null);
    167. Cell cellTable5 = rowTable.createCell(5);
    168. cellTable5.setCellValue(remarks);
    169. if(!columnResultSet.isLast()){
    170. cellTable0.setCellStyle(columnCellStyle(workbook));
    171. cellTable1.setCellStyle(columnCellStyle(workbook));
    172. cellTable2.setCellStyle(columnCellStyle(workbook));
    173. cellTable3.setCellStyle(columnCellStyle(workbook));
    174. cellTable4.setCellStyle(columnCellStyle(workbook));
    175. cellTable5.setCellStyle(columnCellStyle(workbook,"1"));
    176. }else{
    177. cellTable0.setCellStyle(columnCellStyle(workbook,"2"));
    178. cellTable1.setCellStyle(columnCellStyle(workbook,"2"));
    179. cellTable2.setCellStyle(columnCellStyle(workbook,"2"));
    180. cellTable3.setCellStyle(columnCellStyle(workbook,"2"));
    181. cellTable4.setCellStyle(columnCellStyle(workbook,"2"));
    182. cellTable5.setCellStyle(columnCellStyle(workbook,"3"));
    183. }
    184. j++;
    185. }
    186. pkColumns.close();
    187. columnResultSet.close();
    188. } catch (SQLException throwables) {
    189. throwables.printStackTrace();
    190. }
    191. }
    192. OutputStream os = null;
    193. try {
    194. os = new FileOutputStream("d:\\"+schema+".xlsx");
    195. workbook.write(os);
    196. os.close();
    197. } catch (Exception e) {
    198. e.printStackTrace();
    199. }
    200. } catch (SQLException throwables) {
    201. throwables.printStackTrace();
    202. }
    203. }
    204. //索引sheet样式设置
    205. private static CellStyle indexCellStyle(XSSFWorkbook workbook, String type){
    206. CellStyle cellStyle = workbook.createCellStyle();
    207. cellStyle.setBorderBottom(BorderStyle.THIN);
    208. cellStyle.setBorderLeft(BorderStyle.THIN);
    209. cellStyle.setBorderRight(BorderStyle.THIN);
    210. cellStyle.setBorderTop(BorderStyle.THIN);
    211. //设置字体
    212. Font font = workbook.createFont();
    213. font.setFontName("宋体");
    214. font.setFontHeightInPoints((short) 11);
    215. if("2".equals(type)){
    216. font.setUnderline((byte) 1);
    217. font.setColor(IndexedColors.BLUE.index);
    218. }
    219. cellStyle.setFont(font);
    220. return cellStyle;
    221. }
    222. //详细表sheet样式设置
    223. private static CellStyle detailCellStyle(XSSFWorkbook workbook){
    224. XSSFCellStyle cellStyle = workbook.createCellStyle();
    225. XSSFColor color=new XSSFColor(new java.awt.Color(155,194,230),new DefaultIndexedColorMap());
    226. cellStyle.setFillForegroundColor(color);
    227. cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    228. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    229. cellStyle.setBorderTop(BorderStyle.MEDIUM);
    230. cellStyle.setTopBorderColor(IndexedColors.BLUE.index);
    231. //设置字体
    232. Font font = workbook.createFont();
    233. font.setFontName("宋体");
    234. font.setFontHeightInPoints((short) 11);
    235. cellStyle.setFont(font);
    236. return cellStyle;
    237. }
    238. private static CellStyle detailCellStyle(XSSFWorkbook workbook,String type){
    239. CellStyle cellStyle = detailCellStyle(workbook);
    240. cellStyle.setBorderRight(BorderStyle.MEDIUM);
    241. cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
    242. return cellStyle;
    243. }
    244. //设置字段样式
    245. private static CellStyle columnCellStyle(XSSFWorkbook workbook){
    246. CellStyle cellStyle = workbook.createCellStyle();
    247. //设置字体
    248. Font font = workbook.createFont();
    249. font.setFontName("宋体");
    250. font.setFontHeightInPoints((short) 11);
    251. cellStyle.setFont(font);
    252. return cellStyle;
    253. }
    254. private static CellStyle columnCellStyle(XSSFWorkbook workbook,String type){
    255. CellStyle cellStyle = columnCellStyle(workbook);
    256. //右边框
    257. if("1".equals(type)){
    258. cellStyle.setBorderRight(BorderStyle.MEDIUM);
    259. cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
    260. }
    261. //底边框
    262. else if("2".equals(type)){
    263. cellStyle.setBorderBottom(BorderStyle.MEDIUM);
    264. cellStyle.setBottomBorderColor(IndexedColors.BLUE.index);
    265. }
    266. //表格最后一行需同时设置右边框和底边框
    267. else{
    268. cellStyle.setBorderRight(BorderStyle.MEDIUM);
    269. cellStyle.setRightBorderColor(IndexedColors.BLUE.index);
    270. cellStyle.setBorderBottom(BorderStyle.MEDIUM);
    271. cellStyle.setBottomBorderColor(IndexedColors.BLUE.index);
    272. }
    273. return cellStyle;
    274. }
    275. //设置主页sheet列宽
    276. private static void setColumnWidth(XSSFSheet sheet,int index){
    277. for(int i = 0; i<=index; i++){
    278. sheet.setColumnWidth(i, 30 * 256);
    279. }
    280. }
    281. //设置详情sheet列宽
    282. private static void setDetailColumnWidth(XSSFSheet sheet){
    283. sheet.setColumnWidth(0, 30 * 256);
    284. sheet.setColumnWidth(1, 30 * 256);
    285. sheet.setColumnWidth(2, 15 * 256);
    286. sheet.setColumnWidth(3, 15 * 256);
    287. sheet.setColumnWidth(4, 15 * 256);
    288. sheet.setColumnWidth(5, 30 * 256);
    289. }
    290. //使用静态内部类(在初始化这个内部类的时候,JLS(Java Language Sepcification)会保证这个类的线程安全)
    291. private static class SingleTonHolder{
    292. public final static XSSFWorkbook singleTon = new XSSFWorkbook();
    293. }
    294. public static XSSFWorkbook getInstance(){
    295. return SingleTonHolder.singleTon;
    296. }
    297. }

  • 相关阅读:
    快手安全 X 墨菲安全 | 软件供应链安全解决方案完整分享
    nvm安装后node或npm不是内部或外部命令
    web前端课程设计——重庆旅游7页 HTML+CSS+JavaScript
    书生·浦语大模型实战营第二期作业六
    设置网站置灰 css / js
    创建n维空间每个维度(轴)的刻度值numpy.ogrid[]
    视频拉流推流技术梳理
    PC5080USB适配器充电芯片5V/1A输入具有0V充电功能
    过滤器Filter和拦截器Interceptor实现登录校验
    设计模式:命令模式
  • 原文地址:https://blog.csdn.net/jesson_c/article/details/127764206