use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class MetaUtil method getTables.
/**
* 获得所有表名
*
* @param ds 数据源
* @param schema 表数据库名,对于Oracle为用户名
* @param tableName 表名
* @param types 表类型
* @return 表名列表
* @since 3.3.1
*/
public static List<String> getTables(DataSource ds, String schema, String tableName, TableType... types) {
final List<String> tables = new ArrayList<>();
Connection conn = null;
try {
conn = ds.getConnection();
// catalog和schema获取失败默认使用null代替
String catalog = getCataLog(conn);
if (null == schema) {
schema = getSchema(conn);
}
final DatabaseMetaData metaData = conn.getMetaData();
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) {
if (null != rs) {
String table;
while (rs.next()) {
table = rs.getString("TABLE_NAME");
if (StrUtil.isNotBlank(table)) {
tables.add(table);
}
}
}
}
} catch (Exception e) {
throw new DbRuntimeException("Get tables error!", e);
} finally {
DbUtil.close(conn);
}
return tables;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class MetaUtil method getColumnNames.
/**
* 获得结果集的所有列名
*
* @param rs 结果集
* @return 列名数组
* @throws DbRuntimeException SQL执行异常
*/
public static String[] getColumnNames(ResultSet rs) throws DbRuntimeException {
try {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String[] labelNames = new String[columnCount];
for (int i = 0; i < labelNames.length; i++) {
labelNames[i] = rsmd.getColumnLabel(i + 1);
}
return labelNames;
} catch (Exception e) {
throw new DbRuntimeException("Get colunms error!", e);
}
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class MongoDS method initSingle.
/**
* 初始化<br>
* 设定文件中的host和端口有三种形式:
*
* <pre>
* host = host:port
* </pre>
*
* <pre>
* host = host
* port = port
* </pre>
*
* <pre>
* host = host
* </pre>
*/
public synchronized void initSingle() {
if (setting == null) {
try {
setting = new Setting(MONGO_CONFIG_PATH, true);
} catch (Exception e) {
// 在single模式下,可以没有配置文件。
}
}
String group = StrUtil.EMPTY;
if (null == this.serverAddress) {
// 存在唯一分组
if (groups != null && groups.length == 1) {
group = groups[0];
}
serverAddress = createServerAddress(group);
}
final MongoCredential credentail = createCredentail(group);
try {
if (null == credentail) {
mongo = new MongoClient(serverAddress, buildMongoClientOptions(group));
} else {
mongo = new MongoClient(serverAddress, credentail, buildMongoClientOptions(group));
}
} catch (Exception e) {
throw new DbRuntimeException(StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e);
}
log.info("Init MongoDB pool with connection to [{}]", serverAddress);
}
Aggregations