免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7270 | 回复: 4
打印 上一主题 下一主题

读取mysql表注释的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-27 17:02 |只看该作者 |倒序浏览
我给mysql表加了个注释,
请问用java怎么把它读出来呢?

用DatabaseMetaData.getTables()方法好象读出注释来都是空的啊!

论坛徽章:
0
2 [报告]
发表于 2006-04-28 17:57 |只看该作者
根据情况自己改改



  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;

  11. public class MySQLTableComment {

  12.         public static Connection getMySQLConnection() throws Exception {
  13.                 Class.forName("com.mysql.jdbc.Driver");
  14.                 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name","user","password");
  15.                 return conn;
  16.         }
  17.        
  18.         public static Map getCommentByTableName(List tableName) throws Exception {
  19.                 Map map = new HashMap();
  20.                 Connection conn = getMySQLConnection();
  21.                 Statement stmt = conn.createStatement();
  22.                 for(int i = 0; i < tableName.size(); i++) {
  23.                         String table = (String)tableName.get(i);
  24.                         ResultSet rs = stmt.executeQuery("SHOW CREATE TABLE " + table);
  25.                         if(rs != null && rs.next()) {
  26.                                 String create = rs.getString(2);
  27.                                 String comment = parse(create);
  28.                                 map.put(table,comment);
  29.                         }
  30.                         rs.close();
  31.                 }
  32.                 stmt.close();
  33.                 conn.close();
  34.                 return map;
  35.         }
  36.        
  37.         public static List getAllTableName() throws Exception{
  38.                 List tables = new ArrayList();
  39.                 Connection conn = getMySQLConnection();
  40.                 Statement stmt = conn.createStatement();
  41.                 ResultSet rs = stmt.executeQuery("SHOW TABLES ");
  42.                 while(rs.next()) {
  43.                         String tableName = rs.getString(1);
  44.                         tables.add(tableName);
  45.                 }
  46.                 rs.close();
  47.                 stmt.close();
  48.                 conn.close();
  49.                 return tables;
  50.         }
  51.        
  52.         public static String parse(String all) {
  53.                 String comment = null;
  54.                 int index = all.indexOf("COMMENT='");
  55.                 if(index < 0) {
  56.                         return "";
  57.                 }
  58.                 comment = all.substring(index+9);
  59.                 comment = comment.substring(0,comment.length() - 1);
  60.                 return comment;
  61.         }
  62.        
  63.         public static void main(String[] args) throws Exception{
  64.                 List tables = getAllTableName();
  65.                 Map tablesComment = getCommentByTableName(tables);
  66.                 Set names = tablesComment.keySet();
  67.                 Iterator iter = names.iterator();
  68.                 while(iter.hasNext()) {
  69.                         String name = (String)iter.next();
  70.                         System.out.println("Table Name: " + name + ", Comment: " + tablesComment.get(name));
  71.                 }
  72.         }

  73. }

复制代码

论坛徽章:
0
3 [报告]
发表于 2006-04-28 17:59 |只看该作者
server version: 5.0.18-nt

论坛徽章:
0
4 [报告]
发表于 2006-04-29 10:37 |只看该作者
多谢ruknow 兄!

问题已解决了,思路就是这样的!

另外我还用SHOW TABLE STATUS来做也不错!

[ 本帖最后由 tangchaodong 于 2006-4-29 10:40 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2006-04-29 13:40 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP