Search in sources :

Example 11 with DbRuntimeException

use of cn.hutool.db.DbRuntimeException 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)

Example 12 with DbRuntimeException

use of cn.hutool.db.DbRuntimeException in project hutool by looly.

the class DriverUtil method identifyDriver.

/**
 * 识别JDBC驱动名
 *
 * @param conn 数据库连接对象
 * @return 驱动
 * @throws DbRuntimeException SQL异常包装,获取元数据信息失败
 */
public static String identifyDriver(Connection conn) throws DbRuntimeException {
    String driver;
    DatabaseMetaData meta;
    try {
        meta = conn.getMetaData();
        driver = identifyDriver(meta.getDatabaseProductName());
        if (StrUtil.isBlank(driver)) {
            driver = identifyDriver(meta.getDriverName());
        }
    } catch (SQLException e) {
        throw new DbRuntimeException("Identify driver error!", e);
    }
    return driver;
}
Also used : SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 13 with DbRuntimeException

use of cn.hutool.db.DbRuntimeException in project hutool by looly.

the class DbcpDSFactory method createDataSource.

/**
 * 创建数据源
 * @param group 分组
 * @return Dbcp数据源 {@link BasicDataSource}
 */
private BasicDataSource createDataSource(String group) {
    final Setting config = setting.getSetting(group);
    if (CollectionUtil.isEmpty(config)) {
        throw new DbRuntimeException("No DBCP config for group: [{}]", group);
    }
    final BasicDataSource ds = new BasicDataSource();
    // 基本信息
    String url = config.getAndRemoveStr(KEY_ALIAS_URL);
    if (StrUtil.isBlank(url)) {
        throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
    }
    ds.setUrl(url);
    ds.setUsername(config.getAndRemoveStr(KEY_ALIAS_USER));
    ds.setPassword(config.getAndRemoveStr(KEY_ALIAS_PASSWORD));
    final String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
    if (StrUtil.isNotBlank(driver)) {
        ds.setDriverClassName(driver);
    } else {
        ds.setDriverClassName(DbUtil.identifyDriver(ds.getUrl()));
    }
    // 注入属性
    config.toBean(ds);
    return ds;
}
Also used : Setting(cn.hutool.setting.Setting) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 14 with DbRuntimeException

use of cn.hutool.db.DbRuntimeException in project hutool by looly.

the class HikariDSFactory method createDataSource.

/**
 * 创建数据源
 * @param group 分组
 * @return Hikari数据源 {@link HikariDataSource}
 */
private HikariDataSource createDataSource(String group) {
    if (group == null) {
        group = StrUtil.EMPTY;
    }
    final Properties config = setting.getProperties(group);
    if (CollectionUtil.isEmpty(config)) {
        throw new DbRuntimeException("No HikariCP config for group: [{}]", group);
    }
    // 规范化属性名
    if (false == config.containsKey("jdbcUrl") && config.containsKey("url")) {
        config.put("jdbcUrl", config.remove("url"));
    }
    if (false == config.containsKey("username") && config.containsKey("user")) {
        config.put("username", config.remove("user"));
    }
    if (false == config.containsKey("password") && config.containsKey("pass")) {
        config.put("password", config.remove("pass"));
    }
    if (false == config.containsKey("driverClassName") && config.containsKey("driver")) {
        config.put("driverClassName", config.remove("driver"));
    }
    final HikariDataSource ds = new HikariDataSource(new HikariConfig(config));
    return ds;
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Properties(java.util.Properties) HikariConfig(com.zaxxer.hikari.HikariConfig) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 15 with DbRuntimeException

use of cn.hutool.db.DbRuntimeException in project hutool by looly.

the class SqlUtil method createBlob.

/**
 * 创建Blob对象
 *
 * @param conn          {@link Connection}
 * @param dataStream    数据流,使用完毕后关闭
 * @param closeAfterUse 使用完毕是否关闭流
 * @return {@link Blob}
 * @since 4.5.13
 */
public static Blob createBlob(Connection conn, InputStream dataStream, boolean closeAfterUse) {
    Blob blob;
    OutputStream out = null;
    try {
        blob = conn.createBlob();
        out = blob.setBinaryStream(1);
        IoUtil.copy(dataStream, out);
    } catch (SQLException e) {
        throw new DbRuntimeException(e);
    } finally {
        IoUtil.close(out);
        if (closeAfterUse) {
            IoUtil.close(dataStream);
        }
    }
    return blob;
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) OutputStream(java.io.OutputStream) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Aggregations

DbRuntimeException (cn.hutool.db.DbRuntimeException)23 Setting (cn.hutool.setting.Setting)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 NotInitedException (cn.hutool.core.exceptions.NotInitedException)2 SqlBuilder (cn.hutool.db.sql.SqlBuilder)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 Blob (java.sql.Blob)2 Properties (java.util.Properties)2 StrUtil (cn.hutool.core.util.StrUtil)1 DataSourceWrapper (cn.hutool.db.ds.DataSourceWrapper)1 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 Props (cn.hutool.setting.dialect.Props)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ServerAddress (com.mongodb.ServerAddress)1