Search in sources :

Example 21 with DbRuntimeException

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;
}
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 22 with DbRuntimeException

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);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DbRuntimeException(cn.hutool.db.DbRuntimeException) SQLException(java.sql.SQLException) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 23 with DbRuntimeException

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);
}
Also used : MongoClient(com.mongodb.MongoClient) MongoCredential(com.mongodb.MongoCredential) Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException) NotInitedException(cn.hutool.core.exceptions.NotInitedException) 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