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