{"id":664,"date":"2022-09-27T10:45:06","date_gmt":"2022-09-27T02:45:06","guid":{"rendered":"https:\/\/22pig.com\/?p=664"},"modified":"2022-09-27T10:45:06","modified_gmt":"2022-09-27T02:45:06","slug":"java%e8%af%bb%e5%8f%96mysql%e8%a1%a8%e7%9a%84%e6%b3%a8%e9%87%8a%e5%8f%8a%e5%ad%97%e6%ae%b5%e6%b3%a8%e9%87%8a","status":"publish","type":"post","link":"http:\/\/22pig.com\/?p=664","title":{"rendered":"java\u8bfb\u53d6mysql\u8868\u7684\u6ce8\u91ca\u53ca\u5b57\u6bb5\u6ce8\u91ca"},"content":{"rendered":"<h1 class=\"postTitle\"><span role=\"heading\" aria-level=\"2\">java\u8bfb\u53d6mysql\u8868\u7684\u6ce8\u91ca\u53ca\u5b57\u6bb5\u6ce8\u91ca<\/span><\/h1>\n<p>&nbsp;<\/p>\n<p>\/**<br \/>\n* \u8bfb\u53d6mysql\u67d0\u6570\u636e\u5e93\u4e0b\u8868\u7684\u6ce8\u91ca\u4fe1\u606f<br \/>\n*<br \/>\n* @author xxx<br \/>\n*\/<br \/>\npublic class MySQLTableComment {<br \/>\npublic static Connection getMySQLConnection() throws Exception {<br \/>\nClass.forName(&#8220;com.mysql.jdbc.Driver&#8221;);<br \/>\nConnection conn = DriverManager.getConnection(&#8220;jdbc:mysql:\/\/localhost:3306\/databaseName&#8221;, &#8220;root&#8221;, &#8220;root&#8221;);<br \/>\nreturn conn;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* \u83b7\u53d6\u5f53\u524d\u6570\u636e\u5e93\u4e0b\u7684\u6240\u6709\u8868\u540d\u79f0<br \/>\n* @return<br \/>\n* @throws Exception<br \/>\n*\/<br \/>\npublic static List getAllTableName() throws Exception {<br \/>\nList tables = new ArrayList();<br \/>\nConnection conn = getMySQLConnection();<br \/>\nStatement stmt = conn.createStatement();<br \/>\nResultSet rs = stmt.executeQuery(&#8220;SHOW TABLES &#8220;);<br \/>\nwhile (rs.next()) {<br \/>\nString tableName = rs.getString(1);<br \/>\ntables.add(tableName);<br \/>\n}<br \/>\nrs.close();<br \/>\nstmt.close();<br \/>\nconn.close();<br \/>\nreturn tables;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* \u83b7\u5f97\u67d0\u8868\u7684\u5efa\u8868\u8bed\u53e5<br \/>\n* @param tableName<br \/>\n* @return<br \/>\n* @throws Exception<br \/>\n*\/<br \/>\npublic static Map getCommentByTableName(List tableName) throws Exception {<br \/>\nMap map = new HashMap();<br \/>\nConnection conn = getMySQLConnection();<br \/>\nStatement stmt = conn.createStatement();<br \/>\nfor (int i = 0; i &lt; tableName.size(); i++) {<br \/>\nString table = (String) tableName.get(i);<br \/>\nResultSet rs = stmt.executeQuery(&#8220;SHOW CREATE TABLE &#8221; + table);<br \/>\nif (rs != null &amp;&amp; rs.next()) {<br \/>\nString createDDL = rs.getString(2);<br \/>\nString comment = parse(createDDL);<br \/>\nmap.put(table, comment);<br \/>\n}<br \/>\nrs.close();<br \/>\n}<br \/>\nstmt.close();<br \/>\nconn.close();<br \/>\nreturn map;<br \/>\n}<br \/>\n\/**<br \/>\n* \u83b7\u5f97\u67d0\u8868\u4e2d\u6240\u6709\u5b57\u6bb5\u7684\u6ce8\u91ca<br \/>\n* @param tableName<br \/>\n* @return<br \/>\n* @throws Exception<br \/>\n*\/<br \/>\npublic static void getColumnCommentByTableName(List tableName) throws Exception {<br \/>\nMap map = new HashMap();<br \/>\nConnection conn = getMySQLConnection();<br \/>\nStatement stmt = conn.createStatement();<br \/>\nfor (int i = 0; i &lt; tableName.size(); i++) {<br \/>\nString table = (String) tableName.get(i);<br \/>\nResultSet rs = stmt.executeQuery(&#8220;show full columns from &#8221; + table);<br \/>\nSystem.out.println(&#8220;\u3010&#8221;+table+&#8221;\u3011&#8221;);<br \/>\n\/\/ if (rs != null &amp;&amp; rs.next()) {<br \/>\n\/\/map.put(rs.getString(&#8220;Field&#8221;), rs.getString(&#8220;Comment&#8221;));<br \/>\nwhile (rs.next()) {<br \/>\n\/\/ System.out.println(&#8220;\u5b57\u6bb5\u540d\u79f0\uff1a&#8221; + rs.getString(&#8220;Field&#8221;) + &#8220;\\t&#8221;+ &#8220;\u5b57\u6bb5\u6ce8\u91ca\uff1a&#8221; + rs.getString(&#8220;Comment&#8221;) );<br \/>\nSystem.out.println(rs.getString(&#8220;Field&#8221;) + &#8220;\\t:\\t&#8221;+ rs.getString(&#8220;Comment&#8221;) );<br \/>\n}<br \/>\n\/\/ }<br \/>\nrs.close();<br \/>\n}<br \/>\nstmt.close();<br \/>\nconn.close();<br \/>\n\/\/ return map;<br \/>\n}<\/p>\n<p>&nbsp;<\/p>\n<p>\/**<br \/>\n* \u8fd4\u56de\u6ce8\u91ca\u4fe1\u606f<br \/>\n* @param all<br \/>\n* @return<br \/>\n*\/<\/p>\n<p>public static String parse(String all) {<br \/>\nString comment = null;<br \/>\nint index = all.indexOf(&#8220;COMMENT='&#8221;);<br \/>\nif (index &lt; 0) {<br \/>\nreturn &#8220;&#8221;;<br \/>\n}<br \/>\ncomment = all.substring(index + 9);<br \/>\ncomment = comment.substring(0, comment.length() &#8211; 1);<br \/>\nreturn comment;<br \/>\n}<\/p>\n<p>public static void main(String[] args) throws Exception {<br \/>\nList tables = getAllTableName();<br \/>\nMap tablesComment = getCommentByTableName(tables);<br \/>\nSet names = tablesComment.keySet();<br \/>\nIterator iter = names.iterator();<br \/>\nwhile (iter.hasNext()) {<br \/>\nString name = (String) iter.next();<br \/>\nSystem.out.println(&#8220;Table Name: &#8221; + name + &#8220;, Comment: &#8221; + tablesComment.get(name));<br \/>\n}<\/p>\n<p>getColumnCommentByTableName(tables);<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java\u8bfb\u53d6mysql\u8868\u7684\u6ce8\u91ca\u53ca\u5b57\u6bb5\u6ce8\u91ca &nbsp; \/** * \u8bfb\u53d6mysql\u67d0\u6570\u636e\u5e93\u4e0b\u8868\u7684\u6ce8\u91ca\u4fe1\u606f * * @author xxx *\/ public class MySQLTableComment { public static Connection getMySQLConnection() throws Exception { Class.forName(&#8220;com.mysql.jdbc.Driver&#8221;); Connection conn = DriverManager.getConnection(&#8220;jdbc:mysql:\/\/localhost:3306\/databaseName&#8221;, &#8220;root&#8221;, &#8220;root&#8221;); return conn; } \/** * \u83b7\u53d6\u5f53\u524d\u6570\u636e\u5e93\u4e0b\u7684\u6240\u6709\u8868\u540d\u79f0 * @return * @throws Exception *\/ public static List getAllTableName() throws Exception { List tables = new ArrayList(); Connection conn = &#8230; <a title=\"java\u8bfb\u53d6mysql\u8868\u7684\u6ce8\u91ca\u53ca\u5b57\u6bb5\u6ce8\u91ca\" class=\"read-more\" href=\"http:\/\/22pig.com\/?p=664\" aria-label=\"More on java\u8bfb\u53d6mysql\u8868\u7684\u6ce8\u91ca\u53ca\u5b57\u6bb5\u6ce8\u91ca\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=664"}],"version-history":[{"count":0,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/664\/revisions"}],"wp:attachment":[{"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}