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

利用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;
    }

猜你喜欢

转载自huqiji.iteye.com/blog/2205161
今日推荐