java连接mysql并测试是否成功,并查询当前数据库的所有数据表及对应表的字段信息

在项目开发过程中,涉及到动态配置数据源的功能,页面上配置一个新的数据源,需要在后台做验证是否可以连接,做下记录:

1.获取对应数据表:

/**
     * <数据源验证,验证成功返回对应数据库中的表名list>
     *
     * @param dataSourceCheckDTO dataSourceCheckDTO
     * @return 验证的数据源的表名list
     * @throws
     */
    public List<String> getTables(DataSourceCheckDTO dataSourceCheckDTO)
        throws DataAccessException
    {
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try
        {
            //获取连接
            Class.forName(DriverClassNameEnum.getDriverClassName(
                dataSourceCheckDTO.getDataSourceJdbc().toLowerCase()).getMessage());
            conn = DriverManager.getConnection(dataSourceCheckDTO.getDataSourceAddr(),
                dataSourceCheckDTO.getDataSourceAccount(),
                dataSourceCheckDTO.getDataSourcePassword());

            if (conn.isClosed())
            {
                //连接失败,抛出异常
                logger.error("[getTables] fail to connect data source.");
                throw new DataAccessException("00006021");
            }

            statement = conn.prepareStatement(
                "select table_name from information_schema.tables where table_schema = (select database())");
            resultSet = statement.executeQuery();
            List<String> tableNameList = new ArrayList<>();
            while (resultSet.next())
            {
                tableNameList.add(resultSet.getString("table_name"));
            }

            return tableNameList;
        }
        catch (Exception e)
        {
            logger.error("[getTables] catch exception. cause: ", e);
            throw new DataAccessException(e);
        }
        finally
        {
            try
            {
                if (null != resultSet)
                {
                    resultSet.close();
                }
                if (null != statement)
                {
                    statement.close();
                }
                if (null != conn)
                {
                    conn.close();
                }
            }
            catch (SQLException e)
            {
                logger.error("[getTables] close connection catch error. cause: ", e);
                throw new DataAccessException(e);
            }
        }
    }

2.获取对应数据表中的相关字段:

/**
     * <获取指定数据源指定表下的所有列>
     *
     * @param dataSourceCheckDTO dataSourceCheckDTO
     * @return 结果
     * @throws
     */
    public List<String> getColumns(DataSourceCheckDTO dataSourceCheckDTO)
        throws DataAccessException
    {
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try
        {
            //获取连接
            Class.forName(DriverClassNameEnum.getDriverClassName(
                dataSourceCheckDTO.getDataSourceJdbc().toLowerCase()).getMessage());
            conn = DriverManager.getConnection(dataSourceCheckDTO.getDataSourceAddr(),
                dataSourceCheckDTO.getDataSourceAccount(),
                dataSourceCheckDTO.getDataSourcePassword());

            if (conn.isClosed())
            {
                //连接失败,抛出异常
                logger.error("[getColumns] fail to connect data source.");
                throw new DataAccessException("00006021");
            }

            statement = conn.prepareStatement(
                "select column_name columnName, data_type dataType from information_schema.columns " +
                "where table_name = '" + dataSourceCheckDTO.getDataSourceTable() +
                "' and table_schema = (select database()) order by ordinal_position");
            resultSet = statement.executeQuery();
            List<String> tableNameList = new ArrayList<>();
            while (resultSet.next())
            {
                tableNameList.add(resultSet.getString("columnName"));
            }

            return tableNameList;
        }
        catch (Exception e)
        {
            logger.error("[getColumns] catch exception. cause: ", e);
            throw new DataAccessException(e);
        }
        finally
        {
            try
            {
                if (null != resultSet)
                {
                    resultSet.close();
                }
                if (null != statement)
                {
                    statement.close();
                }
                if (null != conn)
                {
                    conn.close();
                }
            }
            catch (SQLException e)
            {
                logger.error("[getColumns] close connection catch error. cause: ", e);
                throw new DataAccessException(e);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/wangchaox123/article/details/94411024