Search in sources :

Example 1 with DataSourceWrapper

use of cn.hutool.db.ds.DataSourceWrapper in project hutool by looly.

the class DriverUtil method identifyDriver.

/**
 * 识别JDBC驱动名
 *
 * @param ds 数据源
 * @return 驱动
 */
public static String identifyDriver(DataSource ds) {
    if (ds instanceof DataSourceWrapper) {
        final String driver = ((DataSourceWrapper) ds).getDriver();
        if (StrUtil.isNotBlank(driver)) {
            return driver;
        }
    }
    Connection conn = null;
    String driver;
    try {
        try {
            conn = ds.getConnection();
        } catch (SQLException e) {
            throw new DbRuntimeException("Get Connection error !", e);
        } catch (NullPointerException e) {
            throw new DbRuntimeException("Unexpected NullPointException, maybe [jdbcUrl] or [url] is empty!", e);
        }
        driver = identifyDriver(conn);
    } finally {
        DbUtil.close(conn);
    }
    return driver;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DataSourceWrapper(cn.hutool.db.ds.DataSourceWrapper) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Aggregations

DbRuntimeException (cn.hutool.db.DbRuntimeException)1 DataSourceWrapper (cn.hutool.db.ds.DataSourceWrapper)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1