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

The following examples show how to use java.sql.ResultSetMetaData#getSchemaName() . 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: MetaData.java    From DataDefender with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a list of metadata information for columns in the passed
 * ResultSet.
 *
 * @param rs
 * @return
 * @throws SQLException
 */
@Override
public TableMetaData getMetaDataFor(final ResultSet rs) throws SQLException {
    TableMetaData table = null;
    final ResultSetMetaData rsmd = rs.getMetaData();
    for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
        if (table == null) {
            table = new TableMetaData(rsmd.getSchemaName(i), rsmd.getTableName(i));
        }
        table.addColumn(
            i,
            rsmd.getColumnName(i),
            sqlTypeMap.getTypeFrom(rsmd.getColumnType(i)),
            rsmd.getColumnDisplaySize(i),
            false,
            null
        );
    }
    return table;
}
 
Example 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
Source File: TableColumnInfo.java    From reladomo with Apache License 2.0 4 votes vote down vote up
public static TableColumnInfo createTableMetadataWithExtraSelect(Connection connection, String schema, String tableName, String fullyQualifiedTableName)
throws SQLException
{
    TableColumnInfo tableInfo = createTableMetadata(connection, schema, tableName, fullyQualifiedTableName);

    if (tableInfo == null || tableInfo.getColumns().length == 0)
    {
        List columns = new ArrayList();
        ResultSet resultSet = null;

        // Get hold of the table catalog and schema separately from the column metadata in case we have a zero-column table/view
        String catalog = null;
        String dbSchema = null;
        String dbName = null;
        Statement stm = null;
        try
        {
            stm = connection.createStatement();
            resultSet = stm.executeQuery("select * from "+fullyQualifiedTableName+" where 0 = 1");
            resultSet.next();
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            int columnCount = resultSetMetaData.getColumnCount();
            dbName = resultSetMetaData.getTableName(1);
            dbSchema = resultSetMetaData.getSchemaName(1);
            if ("".equals(dbSchema))
            {
                dbSchema = null;
            }
            for(int i=0;i< columnCount; i++)
            {
                int col = i + 1;
                ColumnInfo columnInfo = new ColumnInfo(resultSetMetaData.getColumnName(col),
                        resultSetMetaData.getColumnType(col),
                        resultSetMetaData.getColumnDisplaySize(col),
                        resultSetMetaData.getPrecision(col),
                        resultSetMetaData.getScale(col),
                        col,
                        resultSetMetaData.isNullable(col) == DatabaseMetaData.columnNullable);
                columns.add(columnInfo);
            }
        }
        catch(SQLException e)
        {
            //ignore this; it's acceptable for the table to not be found.
        }
        finally
        {
            close(stm);
            close(resultSet);
        }
        if (columns.size() == 0)
        {
            return null;
        }
        tableInfo = new TableColumnInfo(catalog, dbSchema, dbName, (ColumnInfo[]) columns.toArray(new ColumnInfo[columns.size()]));
    }
    return tableInfo;
}
 
Example 13
Source File: MySQLColumnDefinition41Packet.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
public MySQLColumnDefinition41Packet(final int sequenceId, final ResultSetMetaData resultSetMetaData, final int columnIndex) throws SQLException {
    this(sequenceId, resultSetMetaData.getSchemaName(columnIndex), resultSetMetaData.getTableName(columnIndex), resultSetMetaData.getTableName(columnIndex), 
            resultSetMetaData.getColumnLabel(columnIndex), resultSetMetaData.getColumnName(columnIndex), resultSetMetaData.getColumnDisplaySize(columnIndex), 
            MySQLColumnType.valueOfJDBCType(resultSetMetaData.getColumnType(columnIndex)), resultSetMetaData.getScale(columnIndex));
}
 
Example 14
Source File: QueryToXMLConverter.java    From dbunit-extractor with MIT License 4 votes vote down vote up
private void applySelectionChange(final Project project, final Editor editor,
                                  final ExtractorProperties extractorProperties,
                                  final DbDataSource dataSource, final String query) {
    final String cleanedQuery = query.replaceAll(";$", "");
    try (final GuardedRef<DatabaseConnection> connection = DbImplUtil.getDatabaseConnection(dataSource, DGDepartment.CODE_GENERATION);
         final Statement statement = connection == null ? null : connection.get().getJdbcConnection().createStatement();
         final ResultSet resultSet = statement == null ? null : statement.executeQuery(cleanedQuery)) {

        if (resultSet == null) {
            showPopup(editor, MessageType.ERROR, "Connection error");
            return;
        }

        final ResultSetMetaData metaData = resultSet.getMetaData();

        Set<String> tableNames = getTablesNamesFromQuery(metaData);
        if (tableNames.size() != 1) {
            tableNames = getTablesNamesFromQuery(cleanedQuery);
            if (tableNames.size() != 1) {
                showPopup(editor, MessageType.ERROR, "Only one table queries are supported.");
                return;
            }
        }

        final List<Column> columns = constructColumns(metaData);
        final List<Row> rows = constructRows(metaData, resultSet);
        final String tableName = tableNames.iterator().next();
        final String schema = StringUtil.isNotEmpty(metaData.getSchemaName(1))
                ? metaData.getSchemaName(1)
                : getSchemaName(connection.get(), tableName);

        final ResultSetHelper resultSetHelper =
                new ResultSetHelper(extractorProperties,
                                    schema,
                                    tableName,
                                    columns,
                                    rows);
        final XmlGenerator xmlGenerator =
                new XmlGenerator(extractorProperties, resultSetHelper);
        xmlGenerator.appendRows();

        WriteCommandAction.runWriteCommandAction(project, new Runnable() {
            @Override
            public void run() {
                replaceSelection(editor, xmlGenerator.getOutput());
            }
        });
    } catch (Exception e) {
        showPopup(editor, MessageType.ERROR, e.getLocalizedMessage());
    }
}
 
Example 15
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 16
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 ..
  }
}