mysql DatabaseMetaData 获取table remarks为空的解决办法

2 篇文章 0 订阅
1 篇文章 0 订阅

内容转至:http://blog.sina.com.cn/s/blog_6f7d179e0102uwjm.html


利用java.sql.DatabaseMetaData获取数据库信息时,当数据库为mysql时,使用getTables获取表的信息时 无法获取remarks信息,一直以为是mysql驱动的bug,困扰了几天 今天突然在一外国网站上发现 这并不是以bug,而是获取驱动信息时少设置了一个参数,下面是可以正确获取table的remarks的设置驱动的方法

public Connection getConnection(String username,String password,String host,String port,String database) {
        Connection con = null;
        Properties props =new Properties();
        try {
            //Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://"+host+":"+port+"/"+database+"?useUnicode=true&characterEncoding=UTF8";
           
            props.setProperty("user", username);
            props.setProperty("password", password);
            props.setProperty("remarks", "true");
 //设置可以获取remarks信息 
            props.setProperty("useInformationSchema", "true");//设置可以获取tables remarks信息
            con = DriverManager.getConnection(url, props);
           
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

附带oracle中获取驱动方法
public Connection getConnection(String username,String password,String host,String port,String database) {
        Connection con = null;
        Properties props =new Properties();
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@"+host+":"+port+":"+database;
           
            props.setProperty("user", username);
            props.setProperty("password", password);
            props.setProperty("remarks", "true");
           
            con = DriverManager.getConnection(url, props);
           
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值