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);
}
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations