• jdbc连接获取表名称


    1,Class.forName可以替换为mysql之类其他的数据库驱动

    public Connection connect(String url,String username,String pw, int dbType) throws SQLException {
            Connection con = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return DriverManager.getConnection(url,username,pw);
        }

    2,getTables()第一个参数是数据库名称,如果是oracle为null,目前只测试了oracle和mysql,mysql传数据库名称,oracle传null

    public List<Map<String,Object>> getTableNameList(Connection conn,String username, String databaseName, int databaseType) throws SQLException {
            DatabaseMetaData dbmd = conn.getMetaData();
            if (Constant.DATABASE_TYPE_MYSQL != databaseType){
                databaseName = null;
            }
            ResultSet rs = dbmd.getTables(databaseName, username.toUpperCase(), "%", new String[] { "TABLE" });
            List<Map<String,Object>> tableNameList = new ArrayList<Map<String,Object>>();
            while (rs.next()) {
            Map<String, Object> map = new HashMap<String, Object>(); 
            map.put("TABLE_NAME", rs.getString("TABLE_NAME"));
            tableNameList.add(map);
            }
            return tableNameList;
        }

     3,调用

    Connection con = null;
            List<Map<String,Object>> tableNameList = null;
            try {
                con = connect(url,username,pw,databaseType);
                tableNameList = getTableNameList(con,username, databaseName, databaseType);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                    e.printStackTrace();
                    }
                }
            }
    Connection con = null;
    List<Map<String,Object>> tableNameList = null;
    try {
    con = connect(url,username,pw,databaseType);
    tableNameList = getTableNameList(con,username, databaseName, databaseType);
  • 相关阅读:
    初学mongodb笔记
    git学习【转载】
    json序列化与反序列化
    ES6函数比对ES5函数
    前端js脚本与防止js脚本
    js函数整合队列顺序执行插件
    padding-使用必记
    css小技巧
    三分钟教会你开密码箱
    百度地图Marker优化方案
  • 原文地址:https://www.cnblogs.com/cailijuan/p/9934074.html
Copyright © 2020-2023  润新知