Search in sources :

Example 6 with DbRuntimeException

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

the class DbConfig method init.

// -------------------------------------------------------------------- Constructor end
/**
 * 初始化
 *
 * @param url  jdbc url
 * @param user 用户名
 * @param pass 密码
 */
public void init(String url, String user, String pass) {
    this.url = url;
    this.user = user;
    this.pass = pass;
    this.driver = DriverUtil.identifyDriver(url);
    try {
        Class.forName(this.driver);
    } catch (ClassNotFoundException e) {
        throw new DbRuntimeException(e, "Get jdbc driver from [{}] error!", url);
    }
}
Also used : DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 7 with DbRuntimeException

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

the class SimpleDataSource method init.

/**
 * 初始化
 *
 * @param url jdbc url
 * @param user 用户名
 * @param pass 密码
 * @param driver JDBC驱动类,传入空则自动识别驱动类
 * @since 3.1.2
 */
public void init(String url, String user, String pass, String driver) {
    this.driver = StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url);
    try {
        Class.forName(this.driver);
    } catch (ClassNotFoundException e) {
        throw new DbRuntimeException(e, "Get jdbc driver [{}] error!", driver);
    }
    this.url = url;
    this.user = user;
    this.pass = pass;
}
Also used : DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 8 with DbRuntimeException

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

the class SqlUtil method createBlob.

/**
 * 创建Blob对象
 *
 * @param conn {@link Connection}
 * @param data 数据
 * @return {@link Blob}
 * @since 4.5.13
 */
public static Blob createBlob(Connection conn, byte[] data) {
    Blob blob;
    try {
        blob = conn.createBlob();
        blob.setBytes(0, data);
    } catch (SQLException e) {
        throw new DbRuntimeException(e);
    }
    return blob;
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 9 with DbRuntimeException

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

the class MetaUtil method getColumnNames.

/**
 * 获得表的所有列名
 *
 * @param ds        数据源
 * @param tableName 表名
 * @return 列数组
 * @throws DbRuntimeException SQL执行异常
 */
public static String[] getColumnNames(DataSource ds, String tableName) {
    List<String> columnNames = new ArrayList<>();
    Connection conn = null;
    try {
        conn = ds.getConnection();
        // catalog和schema获取失败默认使用null代替
        String catalog = getCataLog(conn);
        String schema = getSchema(conn);
        final DatabaseMetaData metaData = conn.getMetaData();
        try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
            if (null != rs) {
                while (rs.next()) {
                    columnNames.add(rs.getString("COLUMN_NAME"));
                }
            }
        }
        return columnNames.toArray(new String[0]);
    } catch (Exception e) {
        throw new DbRuntimeException("Get columns error!", e);
    } finally {
        DbUtil.close(conn);
    }
}
Also used : ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) DbRuntimeException(cn.hutool.db.DbRuntimeException) SQLException(java.sql.SQLException) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 10 with DbRuntimeException

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

the class DbSetting method getDbConfig.

/**
 * 获得数据库连接信息
 *
 * @param group 分组
 * @return 分组
 */
public DbConfig getDbConfig(String group) {
    final Setting config = setting.getSetting(group);
    if (MapUtil.isEmpty(config)) {
        throw new DbRuntimeException("No Hutool pool config for group: [{}]", group);
    }
    final DbConfig dbConfig = new DbConfig();
    // 基本信息
    final String url = config.getAndRemoveStr(DSFactory.KEY_ALIAS_URL);
    if (StrUtil.isBlank(url)) {
        throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
    }
    dbConfig.setUrl(url);
    // 自动识别Driver
    final String driver = config.getAndRemoveStr(DSFactory.KEY_ALIAS_DRIVER);
    dbConfig.setDriver(StrUtil.isNotBlank(driver) ? driver : DriverUtil.identifyDriver(url));
    dbConfig.setUser(config.getAndRemoveStr(DSFactory.KEY_ALIAS_USER));
    dbConfig.setPass(config.getAndRemoveStr(DSFactory.KEY_ALIAS_PASSWORD));
    // 连接池相关信息
    dbConfig.setInitialSize(setting.getInt("initialSize", group, 0));
    dbConfig.setMinIdle(setting.getInt("minIdle", group, 0));
    dbConfig.setMaxActive(setting.getInt("maxActive", group, 8));
    dbConfig.setMaxWait(setting.getLong("maxWait", group, 6000L));
    // remarks等特殊配置,since 5.3.8
    String connValue;
    for (String key : DSFactory.KEY_CONN_PROPS) {
        connValue = config.get(key);
        if (StrUtil.isNotBlank(connValue)) {
            dbConfig.addConnProps(key, connValue);
        }
    }
    return dbConfig;
}
Also used : Setting(cn.hutool.setting.Setting) 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