Search in sources :

Example 31 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project hibernate-orm by hibernate.

the class AutoDiscoveryTest method testDialectGetColumnAliasExtractor.

@Test
public void testDialectGetColumnAliasExtractor() throws Exception {
    Session session = openSession();
    final SessionImplementor sessionImplementor = (SessionImplementor) session;
    session.beginTransaction();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            PreparedStatement ps = sessionImplementor.getJdbcCoordinator().getStatementPreparer().prepareStatement(QUERY_STRING);
            ResultSet rs = sessionImplementor.getJdbcCoordinator().getResultSetReturn().extract(ps);
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                String column1Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 1);
                String column2Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 2);
                Assert.assertFalse("bad dialect.getColumnAliasExtractor impl", column1Alias.equals(column2Alias));
            } finally {
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(ps);
            }
        }
    });
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) Session(org.hibernate.Session) Test(org.junit.Test)

Example 32 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jfinal by jfinal.

the class ModelBuilder method build.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static final <T> List<T> build(ResultSet rs, Class<? extends Model> modelClass) throws SQLException, InstantiationException, IllegalAccessException {
    List<T> result = new ArrayList<T>();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    String[] labelNames = new String[columnCount + 1];
    int[] types = new int[columnCount + 1];
    buildLabelNamesAndTypes(rsmd, labelNames, types);
    while (rs.next()) {
        Model<?> ar = modelClass.newInstance();
        Map<String, Object> attrs = ar.getAttrs();
        for (int i = 1; i <= columnCount; i++) {
            Object value;
            if (types[i] < Types.BLOB)
                value = rs.getObject(i);
            else if (types[i] == Types.CLOB)
                value = handleClob(rs.getClob(i));
            else if (types[i] == Types.NCLOB)
                value = handleClob(rs.getNClob(i));
            else if (types[i] == Types.BLOB)
                value = handleBlob(rs.getBlob(i));
            else
                value = rs.getObject(i);
            attrs.put(labelNames[i], value);
        }
        result.add((T) ar);
    }
    return result;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ArrayList(java.util.ArrayList)

Example 33 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jfinal by jfinal.

the class TableBuilder method doBuild.

@SuppressWarnings("unchecked")
private void doBuild(Table table, Connection conn, Config config) throws SQLException {
    table.setColumnTypeMap(config.containerFactory.getAttrsMap());
    if (table.getPrimaryKey() == null) {
        table.setPrimaryKey(config.dialect.getDefaultPrimaryKey());
    }
    String sql = config.dialect.forTableBuilderDoBuild(table.getName());
    Statement stm = conn.createStatement();
    ResultSet rs = stm.executeQuery(sql);
    ResultSetMetaData rsmd = rs.getMetaData();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        String colName = rsmd.getColumnName(i);
        String colClassName = rsmd.getColumnClassName(i);
        Class<?> clazz = javaType.getType(colClassName);
        if (clazz != null) {
            table.setColumnType(colName, clazz);
        } else {
            int type = rsmd.getColumnType(i);
            if (type == Types.BINARY || type == Types.VARBINARY || type == Types.BLOB) {
                table.setColumnType(colName, byte[].class);
            } else if (type == Types.CLOB || type == Types.NCLOB) {
                table.setColumnType(colName, String.class);
            } else {
                table.setColumnType(colName, String.class);
            }
        // core.TypeConverter
        // throw new RuntimeException("You've got new type to mapping. Please add code in " + TableBuilder.class.getName() + ". The ColumnClassName can't be mapped: " + colClassName);
        }
    }
    rs.close();
    stm.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 34 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jOOQ by jOOQ.

the class DefaultDSLContext method fetchLazy.

@Override
public Cursor<Record> fetchLazy(ResultSet rs, DataType<?>... types) {
    try {
        Field<?>[] fields = new Field[types.length];
        ResultSetMetaData meta = rs.getMetaData();
        int columns = meta.getColumnCount();
        for (int i = 0; i < types.length && i < columns; i++) {
            fields[i] = field(meta.getColumnLabel(i + 1), types[i]);
        }
        return fetchLazy(rs, fields);
    } catch (SQLException e) {
        throw new DataAccessException("Error while accessing ResultSet meta data", e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TableField(org.jooq.TableField) Field(org.jooq.Field) SelectField(org.jooq.SelectField) SQLException(java.sql.SQLException) DataAccessException(org.jooq.exception.DataAccessException)

Example 35 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project groovy by apache.

the class GroovyResultSetExtension method toString.

public String toString() {
    try {
        StringBuilder sb = new StringBuilder("[");
        ResultSetMetaData metaData = resultSet.getMetaData();
        int count = metaData.getColumnCount();
        for (int i = 1; i <= count; i++) {
            sb.append(metaData.getColumnName(i));
            sb.append(":");
            Object object = resultSet.getObject(i);
            if (object != null) {
                sb.append(object.toString());
            } else {
                sb.append("[null]");
            }
            if (i < count) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    } catch (SQLException e) {
        //            System.err.println("e.getMessage() = " + e.getMessage());
        return super.toString();
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException)

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