4 Replies Latest reply: Jan 19, 2004 1:11 PM by 807582 RSS

    HELP !! java.sql.DatabaseMetaData again ...

    807582
      hi !

      i've been trying to use the interface java.sql.DatabaseMetaData, with the purpose of read a database schema and see the tables, fields, indexes, etc.

      i'm trying this:
      ----------
      DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver() );
      Connection con = DriverManager.getConnection ( parameters );
      DatabaseMetaData dbmd = con.getMetaData();
      
      String types [] = {"TABLE"};
      
      ResultSet rs = dbmd.getTables( null, "schema" , "tables%" , types );
      
      while ( rs.next() )
      {
          String table = rs.getString ( "table_name" );
          System.out.println( "/nTABLE " + table );
      }
      ....
      ----------
      i think this is correct, but the returned ResultSet by dbmd.getTables(...)[\b] is always [b]null[\b] ... maybe the parameters passed to the method are wrong, i don't know ...

      somebody can help with this problem ??
        • 1. Re: HELP !! java.sql.DatabaseMetaData again ...
          807582
          have a look to the api documentation of DatabaseMetaData (http://java.sun.com/j2se/1.4.2/docs/api/java/sql/DatabaseMetaData.html)

          try something like this:
          ResultSet rs = dbmd.getTables(null, null, "*", null);
          this should work...

          • 2. Re: HELP !! java.sql.DatabaseMetaData again ...
            807582
            it doesn't work !!

            thank's anyway ...
            • 3. Re: HELP !! java.sql.DatabaseMetaData again ...
              807582
              Hello again !

              For example, for examine metadata of a Table:

              /*
              * DatabaseMetaData.getColumns(...), retorna un ResultSet que se interpreta de la siguiente manera:
              * 1) TABLE_CAT String => table catalog (may be null)
              * 2) TABLE_SCHEM String => table schema (may be null)
              * 3) TABLE_NAME String => table name
              * 4) COLUMN_NAME String => column name
              * 5) DATA_TYPE short => SQL type from java.sql.Types
              * 6) TYPE_NAME String => Data source dependent type name, for a UDT the type name is fully qualified
              * 7) COLUMN_SIZE int => column size. For char or date types this is the maximum number of characters, for numeric or decimal types this is precision.
              */

              DatabaseMetaData dbmd= con.getMetaData();
              ResultSet rs = dbmd.getColumns(null, null, tableName, null);
              while (rs.next()) {
                   String columnName= rs.getString(4);
                   int columnType= rs.getShort(5);
                   int sizeLength= rs.getInt(7);
                   .
                   .
                   .
              }


              As say Mr. Thof, read documentation about, this clearly explained (http://java.sun.com/j2se/1.4.2/docs/api/java/sql/DatabaseMetaData.html).

              P.D. thank for dukes !! ... again ;P
              • 4. Re: HELP !! java.sql.DatabaseMetaData again ...
                807582
                Thank's, Thof ... Gracias jpadron !!

                I solve the problem earlier today, passing "%" in the tableNamePattern string ...

                I can go on by myself from here ... I really apreciatted your help !!

                Where are you from guys ??