Search in sources :

Example 36 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project Mycat-Server by MyCATApache.

the class TestClass1 method print.

static void print(String name, ResultSet res) throws SQLException {
    System.out.println(name);
    ResultSetMetaData meta = res.getMetaData();
    //System.out.println( "\t"+res.getRow()+"条记录");
    String str = "";
    for (int i = 1; i <= meta.getColumnCount(); i++) {
        str += meta.getColumnName(i) + "   ";
    //System.out.println( meta.getColumnName(i)+"   ");
    }
    System.out.println("\t" + str);
    str = "";
    while (res.next()) {
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            str += res.getString(i) + "   ";
        }
        System.out.println("\t" + str);
        str = "";
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 37 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jirm by agentgt.

the class JdbcResultSetMapperHelper method mapRow.

public Map<String, Object> mapRow(SqlObjectDefinition<?> definition, ResultSet rs, int rowNum) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    Map<String, Object> mapOfColValues = createColumnMap(columnCount);
    for (int i = 1; i <= columnCount; i++) {
        String columnName;
        if ((columnName = rsmd.getColumnLabel(i)) != null && columnName.contains(".")) {
            Object value = getColumnValue(definition, rs, i);
            List<String> names = Lists.newArrayList(splitter.split(columnName));
            ObjectMapUtils.pushPath(mapOfColValues, names, value);
        } else //else if ( (columnName = rsmd.getColumnName(i)) != null && columnName.contains(".")) {
        //TODO make this work.
        //}
        {
            String key = getColumnKey(definition, rsmd, i);
            Object obj = getColumnValue(definition, rs, i);
            mapOfColValues.put(key, obj);
        }
    }
    return mapOfColValues;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 38 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project DataX by alibaba.

the class SingleTableSplitUtil method genSplitSqlForOracle.

/**
     * support Number and String split
     * */
public static List<String> genSplitSqlForOracle(String splitPK, String table, String where, Configuration configuration, int adviceNum) {
    if (adviceNum < 1) {
        throw new IllegalArgumentException(String.format("切分份数不能小于1. 此处:adviceNum=[%s].", adviceNum));
    } else if (adviceNum == 1) {
        return null;
    }
    String whereSql = String.format("%s IS NOT NULL", splitPK);
    if (StringUtils.isNotBlank(where)) {
        whereSql = String.format(" WHERE (%s) AND (%s) ", whereSql, where);
    } else {
        whereSql = String.format(" WHERE (%s) ", whereSql);
    }
    Double percentage = configuration.getDouble(Key.SAMPLE_PERCENTAGE, 0.1);
    String sampleSqlTemplate = "SELECT * FROM ( SELECT %s FROM %s SAMPLE (%s) %s ORDER BY DBMS_RANDOM.VALUE) WHERE ROWNUM <= %s ORDER by %s ASC";
    String splitSql = String.format(sampleSqlTemplate, splitPK, table, percentage, whereSql, adviceNum, splitPK);
    int fetchSize = configuration.getInt(Constant.FETCH_SIZE, 32);
    String jdbcURL = configuration.getString(Key.JDBC_URL);
    String username = configuration.getString(Key.USERNAME);
    String password = configuration.getString(Key.PASSWORD);
    Connection conn = DBUtil.getConnection(DATABASE_TYPE, jdbcURL, username, password);
    LOG.info("split pk [sql={}] is running... ", splitSql);
    ResultSet rs = null;
    List<Pair<Object, Integer>> splitedRange = new ArrayList<Pair<Object, Integer>>();
    try {
        try {
            rs = DBUtil.query(conn, splitSql, fetchSize);
        } catch (Exception e) {
            throw RdbmsException.asQueryException(DATABASE_TYPE, e, splitSql, table, username);
        }
        if (configuration != null) {
            configuration.set(Constant.PK_TYPE, Constant.PK_TYPE_MONTECARLO);
        }
        ResultSetMetaData rsMetaData = rs.getMetaData();
        while (DBUtil.asyncResultSetNext(rs)) {
            ImmutablePair<Object, Integer> eachPoint = new ImmutablePair<Object, Integer>(rs.getObject(1), rsMetaData.getColumnType(1));
            splitedRange.add(eachPoint);
        }
    } catch (DataXException e) {
        throw e;
    } catch (Exception e) {
        throw DataXException.asDataXException(DBUtilErrorCode.ILLEGAL_SPLIT_PK, "DataX尝试切分表发生错误. 请检查您的配置并作出修改.", e);
    } finally {
        DBUtil.closeDBResources(rs, null, null);
    }
    LOG.debug(JSON.toJSONString(splitedRange));
    List<String> rangeSql = new ArrayList<String>();
    int splitedRangeSize = splitedRange.size();
    // demo: Parameter rangeResult can not be null and its length can not <2. detail:rangeResult=[24999930].
    if (splitedRangeSize >= 2) {
        // warn: oracle Number is long type here
        if (isLongType(splitedRange.get(0).getRight())) {
            BigInteger[] integerPoints = new BigInteger[splitedRange.size()];
            for (int i = 0; i < splitedRangeSize; i++) {
                integerPoints[i] = new BigInteger(splitedRange.get(i).getLeft().toString());
            }
            rangeSql.addAll(RdbmsRangeSplitWrap.wrapRange(integerPoints, splitPK));
            // its ok if splitedRangeSize is 1
            rangeSql.add(RdbmsRangeSplitWrap.wrapFirstLastPoint(integerPoints[0], integerPoints[splitedRangeSize - 1], splitPK));
        } else if (isStringType(splitedRange.get(0).getRight())) {
            // warn: treated as string type
            String[] stringPoints = new String[splitedRange.size()];
            for (int i = 0; i < splitedRangeSize; i++) {
                stringPoints[i] = new String(splitedRange.get(i).getLeft().toString());
            }
            rangeSql.addAll(RdbmsRangeSplitWrap.wrapRange(stringPoints, splitPK, "'", DATABASE_TYPE));
            // its ok if splitedRangeSize is 1
            rangeSql.add(RdbmsRangeSplitWrap.wrapFirstLastPoint(stringPoints[0], stringPoints[splitedRangeSize - 1], splitPK, "'", DATABASE_TYPE));
        } else {
            throw DataXException.asDataXException(DBUtilErrorCode.ILLEGAL_SPLIT_PK, "您配置的DataX切分主键(splitPk)有误. 因为您配置的切分主键(splitPk) 类型 DataX 不支持. DataX 仅支持切分主键为一个,并且类型为整数或者字符串类型. 请尝试使用其他的切分主键或者联系 DBA 进行处理.");
        }
    }
    return rangeSql;
}
Also used : Connection(java.sql.Connection) ArrayList(java.util.ArrayList) DataXException(com.alibaba.datax.common.exception.DataXException) BigInteger(java.math.BigInteger) ResultSetMetaData(java.sql.ResultSetMetaData) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) DataXException(com.alibaba.datax.common.exception.DataXException) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 39 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project MSEC by Tencent.

the class DBUtil method findModeResult.

//��ѯ���ݿ⣬�����������������м�¼��ÿ����¼����һ��map���ֶ�����Ϊkey���ֶ�ֵ��Ϊvalue
public ArrayList<Map<String, Object>> findModeResult(String sql, List<Object> params) throws SQLException {
    ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    int index = 1;
    pstmt = connection.prepareStatement(sql);
    if (params != null && !params.isEmpty()) {
        for (int i = 0; i < params.size(); i++) {
            pstmt.setObject(index++, params.get(i));
        }
    }
    resultSet = pstmt.executeQuery();
    ResultSetMetaData metaData = resultSet.getMetaData();
    int cols_len = metaData.getColumnCount();
    while (resultSet.next()) {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < cols_len; i++) {
            String cols_name = metaData.getColumnName(i + 1);
            Object cols_value = resultSet.getObject(cols_name);
            if (cols_value == null) {
                cols_value = "";
            }
            map.put(cols_name, cols_value);
        }
        list.add(map);
    }
    resultSet.close();
    pstmt.close();
    return list;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 40 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project MSEC by Tencent.

the class DBUtil method findSimpleResult.

//��ѯ���ݿ⣬ֻ����һ����¼���ü�¼���ֶα�����Map�ﷵ�أ��ֶ�����Ϊkey���ֶ�ֵ��Ϊvalue
public Map<String, Object> findSimpleResult(String sql, List<Object> params) throws SQLException {
    Map<String, Object> map = new HashMap<String, Object>();
    int index = 1;
    pstmt = connection.prepareStatement(sql);
    if (params != null && !params.isEmpty()) {
        for (int i = 0; i < params.size(); i++) {
            pstmt.setObject(index++, params.get(i));
        }
    }
    resultSet = pstmt.executeQuery();
    ResultSetMetaData metaData = resultSet.getMetaData();
    int col_len = metaData.getColumnCount();
    if (resultSet.next()) {
        for (int i = 0; i < col_len; i++) {
            String cols_name = metaData.getColumnName(i + 1);
            Object cols_value = resultSet.getObject(cols_name);
            if (cols_value == null) {
                cols_value = "";
            }
            map.put(cols_name, cols_value);
        }
    }
    resultSet.close();
    pstmt.close();
    return map;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9