Package java.sql

Examples of java.sql.ResultSetMetaData


     */
    public static void printResultSet(ResultSet rs, int maxColWidth, boolean printMetadata, PrintStream out) throws SQLException {
        if (maxColWidth < 0) {
            maxColWidth = DEFAULT_MAX_COL_WIDTH;
        }
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        int[] sizes = new int[count];
        StringWriter types = new StringWriter();
        StringWriter columns = new StringWriter();
        for (int i = 1; i <= count; i++) {
            String columnName = rsmd.getColumnName(i);
            String typeName = rsmd.getColumnTypeName(i);
            if (maxColWidth == 0) {
                // Sets the width of the column to the wider of the column name and the column type name.
                sizes[i-1] = Math.max(columnName.length(), typeName.length());
            } else {
                // Sets the width of the column to the wider of the column name and the column display size (which cannot exceed maxColWidth).
                sizes[i-1] = Math.max(Math.max(columnName.length(), typeName.length()), // takes into account the type name width
                                      Math.min(rsmd.getColumnDisplaySize(i), maxColWidth));
            }
            types.write(resizeString(typeName, sizes[i-1]));
            columns.write(resizeString(columnName, sizes[i-1]));
            if (i != count) {
                types.write(SPACER);
View Full Code Here


            ResultSet rs = stat.getResultSet();

            // this will check if the result set is updatable
            rs.getConcurrency();

            ResultSetMetaData meta = rs.getMetaData();
            int columnCount = meta.getColumnCount();
            for (int i = 0; i < columnCount; i++) {
                meta.getCatalogName(i + 1);
                meta.getColumnClassName(i + 1);
                meta.getColumnDisplaySize(i + 1);
                meta.getColumnLabel(i + 1);
                meta.getColumnName(i + 1);
                meta.getColumnType(i + 1);
                meta.getColumnTypeName(i + 1);
                meta.getPrecision(i + 1);
                meta.getScale(i + 1);
                meta.getSchemaName(i + 1);
                meta.getTableName(i + 1);
            }
            while (rs.next()) {
                for (int i = 0; i < columnCount; i++) {
                    rs.getObject(i + 1);
                }
View Full Code Here

                + "', 'sa1', 'abc abc', 'TEST')");
        stat.execute("CREATE LINKED TABLE IF NOT EXISTS LINK_TEST('org.h2.Driver', '" + url1
                + "', 'sa1', 'abc abc', 'TEST')");
        testRow(stat, "LINK_TEST");
        ResultSet rs = stat.executeQuery("SELECT * FROM LINK_TEST");
        ResultSetMetaData meta = rs.getMetaData();
        assertEquals(10, meta.getPrecision(1));
        assertEquals(200, meta.getPrecision(2));

        conn.close();
        conn = DriverManager.getConnection(url2, "sa2", "def def");
        stat = conn.createStatement();

View Full Code Here

            maxColWidth = DEFAULT_MAX_COL_WIDTH;
        }
       
        out.println(query);
       
        ResultSetMetaData rsmd = rs.getMetaData();
        int count = rsmd.getColumnCount();
        int[] sizes = new int[count];
        StringWriter types = new StringWriter();
        StringWriter columns = new StringWriter();
        for (int i = 1; i <= count; i++) {
            String columnName = rsmd.getColumnName(i);
            String typeName = rsmd.getColumnTypeName(i);
            if (maxColWidth == 0) {
                // Sets the width of the column to the wider of the column name and the column type name.
                sizes[i-1] = Math.max(columnName.length(), typeName.length());
            } else {
                // Sets the width of the column to the wider of the column name and the column display size (which cannot exceed maxColWidth).
                sizes[i-1] = Math.max(Math.max(columnName.length(), typeName.length()), // takes into account the type name width
                                      Math.min(rsmd.getColumnDisplaySize(i), maxColWidth));
            }
            types.write(resizeString(typeName, sizes[i-1]));
            columns.write(resizeString(columnName, sizes[i-1]));
            if (i != count) {
                types.write(SPACER);
View Full Code Here

    private void testConvertType() throws SQLException {
        deleteDb("cases");
        Connection conn = getConnection("cases");
        Statement stat = conn.createStatement();
        stat.execute("create table test as select cast(0 as dec(10, 2)) x");
        ResultSetMetaData meta = stat.executeQuery("select * from test").getMetaData();
        assertEquals(2, meta.getPrecision(1));
        assertEquals(2, meta.getScale(1));
        stat.execute("alter table test add column y int");
        stat.execute("drop table test");
        conn.close();
    }
View Full Code Here

     *
     * @param prep the prepared statement
     */
    void queryReadResult(PreparedStatement prep) throws SQLException {
        ResultSet rs = prep.executeQuery();
        ResultSetMetaData meta = rs.getMetaData();
        int columnCount = meta.getColumnCount();
        while (rs.next()) {
            for (int i = 0; i < columnCount; i++) {
                rs.getString(i + 1);
            }
        }
View Full Code Here

        stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255), "
                + "DEC DECIMAL(10,2), BOO BIT, BYE TINYINT, BIN BINARY(100), "
                + "D DATE, T TIME, TS TIMESTAMP, DB DOUBLE, R REAL, L BIGINT, "
                + "O_I INT, SH SMALLINT, CL CLOB, BL BLOB)");
        ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
        ResultSetMetaData meta = rs.getMetaData();
        assertEquals("java.lang.Integer", meta.getColumnClassName(1));
        assertEquals("java.lang.String", meta.getColumnClassName(2));
        assertEquals("java.math.BigDecimal", meta.getColumnClassName(3));
        assertEquals("java.lang.Boolean", meta.getColumnClassName(4));
        assertEquals("java.lang.Byte", meta.getColumnClassName(5));
        assertEquals("[B", meta.getColumnClassName(6));
        assertEquals("java.sql.Date", meta.getColumnClassName(7));
        assertEquals("java.sql.Time", meta.getColumnClassName(8));
        assertEquals("java.sql.Timestamp", meta.getColumnClassName(9));
        assertEquals("java.lang.Double", meta.getColumnClassName(10));
        assertEquals("java.lang.Float", meta.getColumnClassName(11));
        assertEquals("java.lang.Long", meta.getColumnClassName(12));
        assertEquals("java.lang.Integer", meta.getColumnClassName(13));
        assertEquals("java.lang.Short", meta.getColumnClassName(14));
        if (SysProperties.RETURN_LOB_OBJECTS) {
            assertEquals("java.sql.Clob", meta.getColumnClassName(15));
            assertEquals("java.sql.Blob", meta.getColumnClassName(16));
        } else {
            assertEquals("java.io.Reader", meta.getColumnClassName(15));
            assertEquals("java.io.InputStream", meta.getColumnClassName(16));
        }
        rs.moveToInsertRow();
        rs.updateInt(1, 0);
        rs.updateNull(2);
        rs.updateNull("DEC");
View Full Code Here

  }

  @Test public void testGetMetaData() throws Exception {
    ResultSetImpl rs = new ResultSetImpl(exampleResultsMsg2a(),
        statement);
    ResultSetMetaData rmetadata = rs.getMetaData();
    assertEquals(2, rmetadata.getColumnCount());

    String[] columnNames = columnNames();
    String[] dataTypes = dataTypes();
    for (int i = 0; i < 2; i++) {
      assertEquals(columnNames[i], rmetadata.getColumnLabel(i + 1));
      assertEquals(dataTypes[i], rmetadata.getColumnTypeName(i + 1));
    }
    rs.close();
  }
View Full Code Here

        // expected column info.
        List columnName = getBQTRSMetaData1a();
        List columnType = getBQTRSMetaData1b();
        List columnTypeName = getBQTRSMetaData1c();

        ResultSetMetaData rm = cs.getMetaData();
        assertNotNull(rm);
           
        for (int j = 1; j <= rm.getColumnCount(); j++) { 
            assertEquals(" Actual doesn't match with expected. ", columnName.get(j-1), rm.getColumnLabel(j)); //$NON-NLS-1$
            assertEquals(" Actual doesn't match with expected. ", columnType.get(j-1), new Integer(rm.getColumnType(j))); //$NON-NLS-1$
            assertEquals(" Actual doesn't match with expected. ", columnTypeName.get(j-1), rm.getColumnTypeName(j)); //$NON-NLS-1$
        }
       
        cs.close();    
    }
View Full Code Here

        cs.close();    
    }

    @Test public void testFindColumn() throws SQLException {
        ResultSetImpl cs =  helpExecuteQuery();
        ResultSetMetaData rm = cs.getMetaData();
        assertNotNull(rm);
        //assertEquals(" Actual columnName doesn't match with expected. ", 1, cs.findColumn("BQT1.MediumA.IntKey"));           
        assertEquals(" Actual columnName doesn't match with expected. ", 1, cs.findColumn("IntKey"));                       //$NON-NLS-1$ //$NON-NLS-2$
        cs.close();            
    }
View Full Code Here

TOP

Related Classes of java.sql.ResultSetMetaData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.