Search in sources :

Example 76 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jdbc-shards by wplatform.

the class ValueResultSet method getString.

@Override
public String getString() {
    try {
        StatementBuilder buff = new StatementBuilder("(");
        result.beforeFirst();
        ResultSetMetaData meta = result.getMetaData();
        int columnCount = meta.getColumnCount();
        for (int i = 0; result.next(); i++) {
            if (i > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (int j = 0; j < columnCount; j++) {
                buff.appendExceptFirst(", ");
                int t = DataType.getValueTypeFromResultSet(meta, j + 1);
                Value v = DataType.readValue(null, result, j + 1, t);
                buff.append(v.getString());
            }
            buff.append(')');
        }
        result.beforeFirst();
        return buff.append(')').toString();
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 77 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project symmetric-ds by JumpMind.

the class DerbyFunctions method getPrimaryKeyWhereString.

public static String getPrimaryKeyWhereString(String[] pkColumnNames, ResultSet rs) throws SQLException {
    final String AND = " and ";
    ResultSetMetaData metaData = rs.getMetaData();
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < pkColumnNames.length; i++) {
        String columnName = pkColumnNames[i];
        int index = findColumnIndex(metaData, columnName);
        int type = metaData.getColumnType(index);
        if (type != Types.BINARY && type != Types.BLOB && type != Types.LONGVARBINARY && type != Types.VARBINARY) {
            b.append("\"").append(columnName).append("\"=");
            switch(type) {
                case Types.BIT:
                case Types.TINYINT:
                case Types.SMALLINT:
                case Types.INTEGER:
                case Types.BIGINT:
                case Types.FLOAT:
                case Types.REAL:
                case Types.DOUBLE:
                case Types.NUMERIC:
                case Types.DECIMAL:
                case Types.BOOLEAN:
                    b.append(rs.getObject(index));
                    break;
                case Types.CHAR:
                case Types.VARCHAR:
                case Types.LONGVARCHAR:
                    b.append("'").append(rs.getString(index)).append("'");
                    break;
                case Types.DATE:
                case Types.TIMESTAMP:
                    b.append("{ts '");
                    b.append(rs.getString(index));
                    b.append("'}");
                    break;
            }
            b.append(AND);
        }
    }
    b.replace(b.length() - AND.length(), b.length(), "");
    return b.toString();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 78 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project symmetric-ds by JumpMind.

the class DerbyFunctions method appendCsvString.

protected static void appendCsvString(String tableName, String[] columnNames, String[] pkColumnNames, ResultSet rs, StringBuilder builder) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    for (String columnName : columnNames) {
        if (StringUtils.isNotBlank(columnName)) {
            int index = findColumnIndex(metaData, columnName);
            if (index >= 0) {
                int type = metaData.getColumnType(index);
                switch(type) {
                    case Types.BLOB:
                        builder.append(blobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                        builder.append(",");
                        break;
                    case Types.CLOB:
                        builder.append(clobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                        builder.append(",");
                        break;
                    default:
                        builder.append(escape(rs.getString(index)));
                        builder.append(",");
                        break;
                }
            } else {
                builder.append(",");
            }
        } else {
            builder.append(",");
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 79 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project symmetric-ds by JumpMind.

the class MetaDataColumnDescriptor method readColumn.

/**
     * Reads the column from the result set.
     * 
     * @param resultSet
     *            The result set
     * @return The column value or the default value if the column is not
     *         present in the result set
     */
public Object readColumn(ResultSet resultSet) throws SQLException {
    ResultSetMetaData metaData = resultSet.getMetaData();
    int foundIdx = -1;
    for (int idx = 1; (foundIdx < 0) && (idx <= metaData.getColumnCount()); idx++) {
        if (_columnName.equals(metaData.getColumnName(idx).toUpperCase())) {
            foundIdx = idx;
        }
    }
    if (foundIdx > 0) {
        switch(_jdbcType) {
            case Types.BIT:
                return new Boolean(resultSet.getBoolean(foundIdx));
            case Types.INTEGER:
                return new Integer(resultSet.getInt(foundIdx));
            case Types.TINYINT:
                return new Short(resultSet.getShort(foundIdx));
            default:
                return resultSet.getString(foundIdx);
        }
    } else {
        return _defaultValue;
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 80 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jdk8u_jdk by JetBrains.

the class CommonRowSetTests method displayResults.

/*
     * Utility method to display the RowSet and will return the row count
     * it found
     */
protected int displayResults(ResultSet rs) throws SQLException {
    int rows = 0;
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    if (rs != null) {
        rs.beforeFirst();
        while (rs.next()) {
            rows++;
            for (int i = 0; i < cols; i++) {
                System.out.print(rs.getString(i + 1) + " ");
            }
            System.out.println();
        }
    }
    return rows;
}
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