All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wiki.xsx.jg.core.DatabaseAdapter Maven / Gradle / Ivy

Go to download

a generator, from the database tables convert to the Java classes or from the Java classes convert to the database tables. support mysql, oracle, sqlserver and postgresql.

There is a newer version: 1.2.1
Show newest version
package wiki.xsx.jg.core;

import java.sql.*;
import java.util.Properties;

/**
 * 数据库适配器
 */
public class DatabaseAdapter {


    /**
     * 获取对应的数据库实现类对象
     * @param driverClassName 数据库驱动名称
     * @param dbUrl 数据库地址
     * @param userName 数据库用户名
     * @param password 数据库密码
     * @param tableNames 数据库表名称,多个逗号分隔
     * @param isCreateFile 是否为创建文件
     * @return 返回数据库实现类对象
     * @throws Exception 异常信息
     */
    public static DatabaseService getServiceInstance(
            String driverClassName,
            String dbUrl,
            String userName,
            String password,
            String tableNames,
            boolean isCreateFile
    ) throws Exception {
        DatabaseService service;
        Database database = getDatabase(driverClassName, dbUrl, userName, password, tableNames, isCreateFile);
        switch (database.getTypeName()){
            case "mysql":
                service = new MysqlService(database);
                break;
            case "oracle":
                service = new OracleService(database);
                break;
            default:
                throw new Exception("Unsupported database type");
        }
        return service;
    }

    /**
     * 获取数据库对象(创建文件)
     * @param driverClassName 数据库驱动名称
     * @param dbUrl 数据库地址
     * @param userName 数据库用户名
     * @param password 数据库密码
     * @param tableNames 数据库表名称,多个逗号分隔
     * @param isCreateFile 是否为创建文件
     * @return 返回数据库对象
     * @throws SQLException 异常信息
     */
    private static Database getDatabase(
            String driverClassName,
            String dbUrl,
            String userName,
            String password,
            String tableNames,
            Boolean isCreateFile
    ) throws SQLException {
        System.setProperty("jdbc.drivers", driverClassName);
        Properties properties = new Properties();
        properties.put("user", userName);
        properties.put("password", password);
        properties.put("remarksReporting", "true");
        try (Connection connection = DriverManager.getConnection(dbUrl, properties)){
            DatabaseMetaData dbmd = connection.getMetaData();
            Database database = new Database(
                    getDBName(dbUrl),
                    dbmd.getDatabaseProductName().toLowerCase(),
                    userName.toUpperCase(),
                    driverClassName,
                    dbUrl,
                    userName,
                    password
            );
            if (isCreateFile){
                if (tableNames!=null&&tableNames.trim().length()>0){
                    String names[] = tableNames.split(",");
                    for (String name : names){
                        ResultSet result = dbmd.getTables(database.getName(), database.getSchema(), name.toUpperCase(), new String[]{"TABLE"});
                        int row = 0;
                        while (result.next()){
                            database.getTableMap().put(
                                    result.getString("TABLE_NAME").toLowerCase(),
                                    new Table(
                                            result.getString("TABLE_NAME").toLowerCase(),
                                            result.getString("REMARKS")==null?"":result.getString("REMARKS")
                                    )
                            );
                            row = result.getRow();
                        }
                        if (row==0){
                            result.close();
                            throw new SQLException("the table \'" + name + "\' is not exists");
                        }
                        result.close();
                    }
                }else{
                    ResultSet result = dbmd.getTables(database.getName(), database.getSchema(), "%", new String[]{"TABLE"});
                    while (result.next()){
                        database.getTableMap().put(
                                result.getString("TABLE_NAME").toLowerCase(),
                                new Table(
                                        result.getString("TABLE_NAME").toLowerCase(),
                                        result.getString("REMARKS")==null?"":result.getString("REMARKS")
                                )
                        );
                    }
                    result.close();
                }
            }
            return database;
        }
    }

    /**
     * 根据数据库地址获取数据库名称
     * @param dbUrl 数据库名称
     * @return
     */
    private static String getDBName(String dbUrl) {
        if (dbUrl.contains("?")){
            return dbUrl.substring(dbUrl.lastIndexOf("/")+1, dbUrl.indexOf("?"));
        }else{
            return dbUrl.substring(dbUrl.lastIndexOf("/")+1);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy