Java Code Examples for java.sql.ResultSetMetaData#getCatalogName()

The following examples show how to use java.sql.ResultSetMetaData#getCatalogName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DriverTest.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Ignore("not maintaining")
@Test
public void testPreStatementWithCubeData() throws SQLException {
    Driver driver = new Driver();
    Properties info = new Properties();
    info.put("user", "");
    info.put("password", "");
    Connection conn = driver.connect("jdbc:kylin://localhost/default", info);
    PreparedStatement state = conn.prepareStatement("select * from test_kylin_fact where seller_id=?");
    state.setLong(1, 10000001);
    ResultSet resultSet = state.executeQuery();

    ResultSetMetaData metadata = resultSet.getMetaData();
    System.out.println("Metadata:");

    for (int i = 0; i < metadata.getColumnCount(); i++) {
        String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " " + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " " + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " " + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " " + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " " + metadata.getTableName(i + 1);
        System.out.println(metaStr);
    }

    System.out.println("Data:");
    while (resultSet.next()) {
        String dataStr = resultSet.getFloat(1) + " " + resultSet.getInt(2) + " " + resultSet.getInt(3) + " " + resultSet.getLong(4) + " " + resultSet.getDate(5) + " " + resultSet.getString(6);
        System.out.println(dataStr);
    }
}
 
Example 2
Source File: DriverTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private void printResultSetMetaData(ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    System.out.println("Metadata:");

    for (int i = 0; i < metadata.getColumnCount(); i++) {
        String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " "
                + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " "
                + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " "
                + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " "
                + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " "
                + metadata.getTableName(i + 1);
        System.out.println(metaStr);
    }
}
 
Example 3
Source File: ResultSetMetaDataHolder.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public ResultSetMetaDataHolder(ResultSetMetaData m, int i) throws SQLException {
       String catalogName = m.getCatalogName(i);
       String columnClassName = m.getColumnClassName(i);
       int columnDisplaySize = m.getColumnDisplaySize(i);
       String columnLabel = m.getColumnLabel(i);
       String columnName = m.getColumnName(i);
       
       int columnType = m.getColumnType(i);
       String columnTypeName = m.getColumnTypeName(i);
       int precision = m.getPrecision(i);
       int scale = m.getScale(i);
       
       String schemaName = m.getSchemaName(i);
       String tableName = m.getTableName(i);
       
       this.catalogName = catalogName;
       this.columnClassName = columnClassName ;
       this.columnDisplaySize = columnDisplaySize;
       this.columnLabel = columnLabel;
       this.columnName = columnName;
       this.columnType=  columnType;
       this.columnTypeName = columnTypeName;
       this.precision  = precision;
       this.scale = scale;
       this.schemaName = schemaName;
       this.tableName = tableName;    		
}
 
Example 4
Source File: SelectStatementsTester.java    From spanner-jdbc with MIT License 5 votes vote down vote up
private void testSelect(String sql, Object... parameters) throws SQLException {
  PreparedStatement ps = connection.prepareStatement(sql);
  for (int i = 1; i <= parameters.length; i++) {
    ps.setObject(i, parameters[i - 1]);
  }
  try (ResultSet rs = ps.executeQuery()) {
    ResultSetMetaData metadata = rs.getMetaData();
    for (int i = 1; i <= metadata.getColumnCount(); i++) {
      metadata.getColumnClassName(i);
      metadata.getColumnDisplaySize(i);
      metadata.getColumnLabel(i);
      metadata.getColumnName(i);
      metadata.getColumnType(i);
      metadata.getColumnTypeName(i);
      metadata.getPrecision(i);
      metadata.getScale(i);
      metadata.getCatalogName(i);
      metadata.getSchemaName(i);
      metadata.getTableName(i);
      metadata.isNullable(i);
      metadata.isAutoIncrement(i);
      metadata.isCaseSensitive(i);
      metadata.isCurrency(i);
      metadata.isDefinitelyWritable(i);
      metadata.isReadOnly(i);
      metadata.isSearchable(i);
      metadata.isSigned(i);
      metadata.isWritable(i);
    }
    while (rs.next()) {
      // do nothing
    }
  }
}
 
Example 5
Source File: DBTableTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void createTable(){
    try {
        //Quoter qt = SQLIdentifiers.createQuoter(dbmd);
        ResultSet rs = conn.createStatement().executeQuery(context.getSqlSelect());
        ResultSetMetaData rsMeta = rs.getMetaData();
        String aName = rsMeta.getTableName(1);
        String aSchema = rsMeta.getSchemaName(1);
        String aCatalog = rsMeta.getCatalogName(1);
        table = new DBTable(aName, aSchema, aCatalog);
        table.setQuoter(SQLIdentifiers.createQuoter(conn.getMetaData()));
        //table.setQuoter(quoter);
    } catch (SQLException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 6
Source File: DBPrimaryKeyTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void createTable() {
    try {
        //Quoter qt = SQLIdentifiers.createQuoter(dbmd);
        ResultSet rs = conn.createStatement().executeQuery(context.getSqlSelect());
        ResultSetMetaData rsMeta = rs.getMetaData();
        String aName = rsMeta.getTableName(1);
        String aSchema = rsMeta.getSchemaName(1);
        String aCatalog = rsMeta.getCatalogName(1);
        table = new DBTable(aName, aSchema, aCatalog);
    //table.setQuoter(quoter);
    } catch (SQLException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 7
Source File: DriverTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
private void printResultSetMetaData(ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    System.out.println("Metadata:");

    for (int i = 0; i < metadata.getColumnCount(); i++) {
        String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " "
                + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " "
                + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " "
                + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " "
                + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " "
                + metadata.getTableName(i + 1);
        System.out.println(metaStr);
    }
}
 
Example 8
Source File: CrudMeta.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private static List<String> getPrimaryKeys(Connection connection, ResultSetMetaData resultSetMetaData, ColumnDefinitionProvider<JdbcColumnKey> columnDefinitionProvider) throws SQLException {
    List<String> primaryKeys = new ArrayList<String>();

    for(int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
        JdbcColumnKey key = JdbcColumnKey.of(resultSetMetaData, i);
        if (columnDefinitionProvider.getColumnDefinition(key).has(KeyProperty.class)) {
            primaryKeys.add(key.getName());
        }
    }

    if (!primaryKeys.isEmpty()) {
        return primaryKeys;
    }

    String catalogName = resultSetMetaData.getCatalogName(1);
    String schemaName = resultSetMetaData.getSchemaName(1);
    String tableName = resultSetMetaData.getTableName(1);
    
    try {
        ResultSet set = connection.getMetaData().getPrimaryKeys(catalogName, schemaName, tableName);
        try {
            while (set.next()) {
                primaryKeys.add(set.getString("COLUMN_NAME"));
            }
        } finally {
            set.close();
        }
    } catch (SQLSyntaxErrorException e) {
        // ignore likely mysql view issues
    }
    return primaryKeys;
}
 
Example 9
Source File: ResultSetLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDbConnectionF_whenRSMetaInfo_thenCorrect() throws SQLException {
    int columnCount = 0;
    try (PreparedStatement pstmt = dbConnection.prepareStatement("select * from employees", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = pstmt.executeQuery()) {
        ResultSetMetaData metaData = rs.getMetaData();
        columnCount = metaData.getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String catalogName = metaData.getCatalogName(i);
            String className = metaData.getColumnClassName(i);
            String label = metaData.getColumnLabel(i);
            String name = metaData.getColumnName(i);
            String typeName = metaData.getColumnTypeName(i);
            Integer type = metaData.getColumnType(i);
            String tableName = metaData.getTableName(i);
            String schemaName = metaData.getSchemaName(i);
            boolean isAutoIncrement = metaData.isAutoIncrement(i);
            boolean isCaseSensitive = metaData.isCaseSensitive(i);
            boolean isCurrency = metaData.isCurrency(i);
            boolean isDefiniteWritable = metaData.isDefinitelyWritable(i);
            boolean isReadOnly = metaData.isReadOnly(i);
            boolean isSearchable = metaData.isSearchable(i);
            boolean isReadable = metaData.isReadOnly(i);
            boolean isSigned = metaData.isSigned(i);
            boolean isWritable = metaData.isWritable(i);
            int nullable = metaData.isNullable(i);
        }
    }

    assertEquals("column count", 4, columnCount);
}
 
Example 10
Source File: JdbcDatabaseManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public ResultSetColumnMetaData(final ResultSetMetaData rsMetaData, final int j) throws SQLException {
    // @formatter:off
    this(rsMetaData.getSchemaName(j),
         rsMetaData.getCatalogName(j),
         rsMetaData.getTableName(j),
         rsMetaData.getColumnName(j),
         rsMetaData.getColumnLabel(j),
         rsMetaData.getColumnDisplaySize(j),
         rsMetaData.getColumnType(j),
         rsMetaData.getColumnTypeName(j),
         rsMetaData.getColumnClassName(j),
         rsMetaData.getPrecision(j),
         rsMetaData.getScale(j));
    // @formatter:on
}
 
Example 11
Source File: DriverTest.java    From Kylin with Apache License 2.0 4 votes vote down vote up
@Ignore("not maintaining")
@Test
public void testWithCubeData() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    Driver driver = (Driver) Class.forName("org.apache.kylin.kylin.jdbc.Driver").newInstance();
    Properties info = new Properties();
    info.put("user", "");
    info.put("password", "");
    Connection conn = driver.connect("jdbc:kylin://localhost/default", info);

    ResultSet catalogs = conn.getMetaData().getCatalogs();
    while (catalogs.next()) {
        System.out.println(catalogs.getString("TABLE_CAT"));
    }

    ResultSet schemas = conn.getMetaData().getSchemas();
    while (schemas.next()) {
        System.out.println(schemas.getString(1));
        System.out.println(schemas.getString(2));
    }

    ResultSet tables = conn.getMetaData().getTables(null, null, null, null);
    while (tables.next()) {
        String tableName = tables.getString(3);
        assertEquals(tables.getString("TABLE_NAME"), tableName);
        ResultSet columns = conn.getMetaData().getColumns(null, null, tableName, null);

        while (columns.next()) {
            System.out.println(columns.getString("COLUMN_NAME"));
            String column = "";
            for (int i = 0; i < 23; i++) {
                column += columns.getString(i + 1) + ", ";
            }

            System.out.println("Column in table " + tableName + ": " + column);
        }
    }

    for (int j = 0; j < 3; j++) {
        Statement state = conn.createStatement();
        ResultSet resultSet = state.executeQuery("select * from test_kylin_fact");

        ResultSetMetaData metadata = resultSet.getMetaData();
        System.out.println("Metadata:");

        for (int i = 0; i < metadata.getColumnCount(); i++) {
            String metaStr = metadata.getCatalogName(i + 1) + " " + metadata.getColumnClassName(i + 1) + " " + metadata.getColumnDisplaySize(i + 1) + " " + metadata.getColumnLabel(i + 1) + " " + metadata.getColumnName(i + 1) + " " + metadata.getColumnType(i + 1) + " " + metadata.getColumnTypeName(i + 1) + " " + metadata.getPrecision(i + 1) + " " + metadata.getScale(i + 1) + " " + metadata.getSchemaName(i + 1) + " " + metadata.getTableName(i + 1);
            System.out.println(metaStr);
        }

        System.out.println("Data:");
        while (resultSet.next()) {
            String dataStr = resultSet.getFloat(1) + " " + resultSet.getInt(2) + " " + resultSet.getInt(3) + " " + resultSet.getLong(4) + " " + resultSet.getDate(5) + " " + resultSet.getString(6);
            System.out.println(dataStr);
        }
    }
}
 
Example 12
Source File: ResultSetTableModelFactory.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * No longer used.
 *
 * @param rsmd
 * @param metaData
 * @param column
 */
@Deprecated
public static void updateMetaData( final ResultSetMetaData rsmd,
                                   final DefaultTableMetaData metaData,
                                   final int column ) {
  try {
    if ( rsmd.isCurrency( column + 1 ) ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.CURRENCY,
          Boolean.TRUE );
    } else {
      metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.CURRENCY,
          Boolean.FALSE );
    }

    if ( rsmd.isSigned( column + 1 ) ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.SIGNED,
          Boolean.TRUE );
    } else {
      metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.SIGNED,
          Boolean.FALSE );
    }

    final String tableName = rsmd.getTableName( column + 1 );
    if ( tableName != null ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Database.NAMESPACE, MetaAttributeNames.Database.TABLE,
          tableName );
    }
    final String schemaName = rsmd.getSchemaName( column + 1 );
    if ( schemaName != null ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Database.NAMESPACE, MetaAttributeNames.Database.SCHEMA,
          schemaName );
    }
    final String catalogName = rsmd.getCatalogName( column + 1 );
    if ( catalogName != null ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Database.NAMESPACE,
          MetaAttributeNames.Database.CATALOG, catalogName );
    }
    final String label = rsmd.getColumnLabel( column + 1 );
    if ( label != null ) {
      metaData.setColumnAttribute( column, MetaAttributeNames.Formatting.NAMESPACE,
          MetaAttributeNames.Formatting.LABEL, label );
    }
    final int displaySize = rsmd.getColumnDisplaySize( column + 1 );
    metaData.setColumnAttribute( column, MetaAttributeNames.Formatting.NAMESPACE,
        MetaAttributeNames.Formatting.DISPLAY_SIZE, IntegerCache.getInteger( displaySize ) );

    final int precision = rsmd.getPrecision( column + 1 );
    metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.PRECISION,
        IntegerCache.getInteger( precision ) );
    final int scale = rsmd.getScale( column + 1 );
    metaData.setColumnAttribute( column, MetaAttributeNames.Numeric.NAMESPACE, MetaAttributeNames.Numeric.SCALE,
        IntegerCache.getInteger( scale ) );
  } catch ( SQLException sqle ) {
    // It is non-fatal if the meta-data cannot be read from the result set. Drivers are
    // buggy all the time ..
  }
}