Mysql和Oracle 数据库操作工具类

适配Mysql和Oracle数据源

文章目录

    • 1. 适配Mysql和Oracle数据源
    • 2. 适配于Mysql数据源
    • 3. 适配Oeacle数据源

1. 适配Mysql和Oracle数据源

package cn.stylefeng.guns.generator.core.util;
import cn.stylefeng.guns.generator.modular.entity.DatabaseInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Dbutils
 * @Deacription TODO
 * @Author gblfy
 * @Date 2019/6/5 9:30
 * @Version 1.0
 **/
@Slf4j
public class Dbutils {
    /**
     * 查询某个数据库连接的所有表
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:30
     */
    public static List> selectTables(DatabaseInfo dbInfo) {
        ArrayList> list = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();
            
/*****************************Mysql和Oracle数据源不同部分 Start******************************/

            if (jdbcUrl.contains("oracle")) {
                String dbName = dbInfo.getUserName();
                String db = dbName.toUpperCase();
                preparedStatement = conn.prepareStatement("SELECT S.TABLE_NAME AS tableName,S.COMMENTS AS  tableComment  FROM SYS.dba_TAB_comments S  WHERE S.OWNER = '" + db + "'");
            } else if (jdbcUrl.contains("mysql")) {
                int first = jdbcUrl.lastIndexOf("/") + 1;
                int last = jdbcUrl.indexOf("?");
                String dbName = jdbcUrl.substring(first, last);
                preparedStatement = conn.prepareStatement("select TABLE_NAME as tableName,TABLE_COMMENT as tableComment from information_schema.`TABLES` where TABLE_SCHEMA = '" + dbName + "'");
            }
            
 /*****************************Mysql和Oracle数据源不同部分 End******************************/  
 
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String tableName = resultSet.getString("tableName");
                String tableComment = resultSet.getString("tableComment");
                map.put("tableName", tableName);
                map.put("tableComment", tableComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }

    /**
     * 查询某个表的所有字段
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:31
     */
    public static List> getTableFields(DatabaseInfo dbInfo, String tableName) {
        ArrayList> list = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();
            
/*****************************Mysql和Oracle数据源不同部分 Start******************************/       
    
            if (jdbcUrl.contains("oracle")) {
                String dbName = dbInfo.getUserName();
                String db = dbName.toUpperCase();
                String tb = tableName.toUpperCase();
                preparedStatement = conn.prepareStatement("SELECT T.COLUMN_NAME  as columnName,S.COMMENTS as columnComment  FROM SYS.dba_TAB_columns T,SYS.ALL_COL_COMMENTS S  WHERE  T.TABLE_NAME = S.TABLE_NAME  AND T.COLUMN_NAME = S.COLUMN_NAME   AND T.OWNER = '" + db + "' AND T.TABLE_NAME = '" + tb + "'");
            } else if (jdbcUrl.contains("mysql")) {
                int first = jdbcUrl.lastIndexOf("/") + 1;
                int last = jdbcUrl.indexOf("?");
                String dbName = jdbcUrl.substring(first, last);
                preparedStatement = conn.prepareStatement(
                        "select COLUMN_NAME as columnName,COLUMN_COMMENT as columnComment from information_schema.COLUMNS where table_name = '" + tableName + "' and table_schema = '" + dbName + "'");
            }
            
 /*****************************Mysql和Oracle数据源不同部分 End******************************/        
 
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String columnName = resultSet.getString("columnName");
                String columnComment = resultSet.getString("columnComment");
                map.put("columnName", columnName);
                map.put("columnComment", columnComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }
}

2. 适配于Mysql数据源

package cn.stylefeng.guns.generator.core.util;

import cn.stylefeng.guns.generator.modular.entity.DatabaseInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库操作工具类
 *
 * @author fengshuonan
 * @Date 2019/1/13 18:34
 */
@Slf4j
public class DbUtil {

    /**
     * 查询某个数据库连接的所有表
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:30
     */
    public static List> selectTables(DatabaseInfo dbInfo) {
        ArrayList> list = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();

/*****************************Mysql数据源独有部分 Start******************************/

            int first = jdbcUrl.lastIndexOf("/") + 1;
            int last = jdbcUrl.indexOf("?");
            String dbName = jdbcUrl.substring(first, last);
            PreparedStatement preparedStatement = conn.prepareStatement("select TABLE_NAME as tableName,TABLE_COMMENT as tableComment from information_schema.`TABLES` where TABLE_SCHEMA = '" + dbName + "'");

/*****************************Mysql数据源独有部分 End******************************/

            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String tableName = resultSet.getString("tableName");
                String tableComment = resultSet.getString("tableComment");
                map.put("tableName", tableName);
                map.put("tableComment", tableComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }

    /**
     * 查询某个表的所有字段
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:31
     */
    public static List> getTableFields(DatabaseInfo dbInfo, String tableName) {
        ArrayList> list = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();

/*****************************Mysql数据源独有部分 Start******************************/

            int first = jdbcUrl.lastIndexOf("/") + 1;
            int last = jdbcUrl.indexOf("?");
            String dbName = jdbcUrl.substring(first, last);
            PreparedStatement preparedStatement = conn.prepareStatement(
                    "select COLUMN_NAME as columnName,COLUMN_COMMENT as columnComment from information_schema.COLUMNS where table_name = '" + tableName + "' and table_schema = '" + dbName + "'");
                    
/*****************************Mysql数据源独有部分 End******************************/

            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String columnName = resultSet.getString("columnName");
                String columnComment = resultSet.getString("columnComment");
                map.put("columnName", columnName);
                map.put("columnComment", columnComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }
}

3. 适配Oeacle数据源

package cn.stylefeng.guns.generator.core.util;

import cn.stylefeng.guns.generator.modular.entity.DatabaseInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库操作工具类
 *
 * @author fengshuonan
 * @Date 2019/1/13 18:34
 */
@Slf4j
public class DbUtil {

    /**
     * 查询某个数据库连接的所有表
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:30
     */
    public static List> selectTables(DatabaseInfo dbInfo) {
        ArrayList> list = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();
            
/*****************************Oracle数据源独有部分 Start******************************/

            String dbName = dbInfo.getUserName();
            String db = dbName.toUpperCase();
            PreparedStatement preparedStatement = conn.prepareStatement("SELECT S.TABLE_NAME AS tableName,S.COMMENTS AS  tableComment  FROM SYS.dba_TAB_comments S  WHERE S.OWNER = '" + db + "'");
            
/*****************************Oracle数据源独有部分 End******************************/

            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String tableName = resultSet.getString("tableName");
                String tableComment = resultSet.getString("tableComment");
                map.put("tableName", tableName);
                map.put("tableComment", tableComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }

    /**
     * 查询某个表的所有字段
     *
     * @author fengshuonan
     * @Date 2019-05-04 20:31
     */
    public static List> getTableFields(DatabaseInfo dbInfo, String tableName) {
        ArrayList> list = new ArrayList<>();
        try {
            Class.forName(dbInfo.getJdbcDriver());
            Connection conn = DriverManager.getConnection(dbInfo.getJdbcUrl(), dbInfo.getUserName(), dbInfo.getPassword());
            String jdbcUrl = dbInfo.getJdbcUrl();
            
/*****************************Oracle数据源独有部分 Start******************************/

            String dbName = dbInfo.getUserName();
            String db = dbName.toUpperCase();
            String tb = tableName.toUpperCase();
            PreparedStatement preparedStatement = conn.prepareStatement("SELECT T.COLUMN_NAME  as columnName,S.COMMENTS as columnComment  FROM SYS.dba_TAB_columns T,SYS.ALL_COL_COMMENTS S  WHERE  T.TABLE_NAME = S.TABLE_NAME  AND T.COLUMN_NAME = S.COLUMN_NAME   AND T.OWNER = '" + db + "' AND T.TABLE_NAME = '" + tb + "'");
            
/*****************************Oracle数据源独有部分 End******************************/

            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                HashMap map = new HashMap<>();
                String columnName = resultSet.getString("columnName");
                String columnComment = resultSet.getString("columnComment");
                map.put("columnName", columnName);
                map.put("columnComment", columnComment);
                list.add(map);
            }
            return list;
        } catch (Exception ex) {
            log.error("执行sql出现问题!", ex);
            return null;
        }
    }

}

你可能感兴趣的:(Guns,后台管理框架)