DatabaseMetaData getImportedKeys

Hi All,
I am trying to fetch the primary and foreign keys of a table. But the getImportedKeys() method always returns empty result set.I have the primary key set on the table. I am using the following code.
ResultSet rs = dmdata.getImportedKeys(null,dbname,tableName);
     while(rs.next()) {
     String pkTable = rs.getString("PKTABLE_NAME");
String pkColumn = rs.getString("PKCOLUMN_NAME"); /* here */
String fkTable = rs.getString("FKTABLE_NAME");
String fkColumn = rs.getString("FKCOLUMN_NAME"); /* and here */
The result set is empty with the above code
Any suggestions would be of great help.
Thanks,
Ashok Vytla

Hi I am using Oracle 9i and using the following java code
DatabaseMetaData dmdata=connection.getMetaData();
//ResultSet rs = dmdata.getImportedKeys(null,dbname,tableName);
ResultSet rs = dmdata.getPrimaryKeys(null,null,tableName);
System.out.println("primaryKey--> rs.next() "+rs.next());
     while(rs.next()) {
     String pkTable = rs.getString("PKTABLE_NAME");
     System.out.println("primaryKey--> pkTable "+pkTable);
     String pkColumn = rs.getString("PKCOLUMN_NAME"); /* here */
     System.out.println("primaryKey--> pkColumn "+pkColumn );
     String fkTable = rs.getString("FKTABLE_NAME");
     System.out.println("primaryKey--> fkTable "+fkTable);
     String fkColumn = rs.getString("FKCOLUMN_NAME"); /* and here */
     System.out.println("primaryKey--> fkColumn "+fkColumn);
//the above result always returns empty rows

Similar Messages

  • Delete Set Default Action

    Would be nice to see a feature introduced in the preferences to set the default action of the delete button, I personally would like it permanently set to "Delete from Disk" and not "Remove"

    Dear StuDerby,
    thank you very much indeed for your helpful reply!! You have again helped me much with your constructive suggestions and your obviously profound JDBC knowledge.
    So what you suggest is:
    String deleteParameter="";
    resultSetImportedKeys = DataBaseMetaData.getImportedKeys(null, null, tableName);
    while(resultSetImportedKeys.next()) {
       String deleteRule = resultSetImportedKeys.getString(11);
       switch (java.lang.Integer.parseInt(deleteRule.trim())) {
          case (DataBaseMetaData.importedKeyCascade): deleteParameter="ON DELETE CASCADE"; break;
          case (DataBaseMetaData.importedKeyRestrict): deleteParameter="ON DELETE RESTRICT"; break;
          case (DataBaseMetaData.importedKeySetDefault): deleteParameter="ON DELETE SET DEFAULT"; break;
          case (DataBaseMetaData.importedKeySetNull): deleteParameter="ON DELETE SET NULL"; break;
          case (DataBaseMetaData.importedKeyNoAction): deleteParameter="ON DELETE NO ACTION"; break;
          default: deleteParameter="ON DELETE NO ACTION"; break;
    }But what are importedKeyInitiallyDeferred, importedKeyInitiallyImmediate, and importedKeyNotDeferrable for?

  • Problem with getImportedKeys() in MySQL

    Hi everyone,
    I am using JDK1.5.0 and the jdbc "mysql-connector-java-3.0.16"
    I've been trying to get the foreign keys from a MySQL 4.1 database using the getImportedKeys() method of the DatabaseMetaData class,but the method returns a ResultSet,which is null!
    However, I noticed that the getPrimaryKeys() method (which needs the same arguments as the getImportedKeys() one) works just fine and that puzzles me.
    Can anybody help me? Is this a known bug of MySQL?
    Thanks in advance.

    Indeed,previous MySQL versions did not support foreign keys...
    However,MySQL Server 4.1 allows foreign keys as you can see:
    CREATE TABLE `companies` (
    `Company_Id` int(6) unsigned NOT NULL auto_increment,
    `Name` varchar(11) default NULL,
    `Artist_Id` int(6) unsigned NOT NULL default '0',
    PRIMARY KEY (`Company_Id`),
    KEY `Artist_Id` (`Artist_Id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    INSERT INTO `companies` VALUES (1,'heaven',1);
    INSERT INTO `companies` VALUES (2,'emi',3);
    # Foreign keys for table companies
    ALTER TABLE `companies`
    ADD FOREIGN KEY (`Artist_Id`) REFERENCES `artists` (`Artist_Id`);
    Any further help? :-)

  • Problem with DatabaseMetaData.getCrossReference()

    Hi, All,
    I got a problem with DatabaseMetaData.getCrossReference(...). Let me simplify it to the following senario:
    I have two tables in RDBMS: table A with colums A1, A2, A3, A4 and table B with columns B1, B2. Table B has a composite primary key (B1, B2). And I have two references:
    1. (A1, A4) references (B1, B2)
    2. (A3, A2) references (B1, B2)
    In this cases, it seems to me that there is no way in JDBC to detect these relations. All the api's of getCrossReference(), getImportedKeys() and getExportedKeys() are not working!!!
    I appreciate your help!
    Thanks
    Tao
    2003/06/19

    Could somebody help me out?
    Thanks in advance!
    Tao

  • OJDBC DatabaseMetaData getExportedKeys very slow on 10g

    We're running against Oracle 10g with the latest ojdbc14_g.jar. We also use Hibernate 1.9.4 and when we call database.createDatabaseSnapshot the application takes almost 4 hours. I tracked it down through hibernate to where databaseMetaData.getExportedKeys is called for every table and I found that each call takes ~3 minutes.
    I found this forum post: getImportedKeys/getExportedKeys slow! but changing the optmizer_mode and rebuilding the temp tablespace didn't seem to improve performance. The other parameters mentioned seemed fine (to my very inexperienced eye). I also tried rebuilding the system statistics.
    Any suggestions would be more than appreciated!
    Thanks,
    Adam Griffis

    >
    We're running against Oracle 10g with the latest ojdbc14_g.jar. We also use Hibernate 1.9.4 and when we call database.createDatabaseSnapshot the application takes almost 4 hours. I tracked it down through hibernate to where databaseMetaData.getExportedKeys is called for every table and I found that each call takes ~3 minutes.
    >
    That is likely to be a Hibernate issue.
    Tests performed on 11g using ojdbc6 jar to directly execute the 'getExportedKeys' method consistently shows about 5 seconds per table for me.
    Create your own test file to measure the Java timing for that method call and post your results
        String schema;
        String tablename;
        try {
            con = . . . get a connection
            DatabaseMetaData dbmd = con.getMetaData();
            long start = System.currentTimeMillis();
            rs = dbmd.getExportedKeys("", schema, tablename);
            long stop = System.currentTimeMillis();
            System.out.println(stop - start);
        } catch (SQLException ex) {
            System.out.println("error code : " + ex.getErrorCode());
            System.out.println("error message : " + ex.getMessage());
        } finally {
            . . . do any clean up
    }The time taken above will tell you if it is a JDBC issue which is unlikely in my opinion.
    The time may be taken by Hibernate iterating the result set and creating all of the class instances.
    You may want to post in a hibernate forum.

  • Call driver methods DatabaseMetaData#getIndexInfo()

    Hi
    got errors when I call driver methods DatabaseMetaData#getIndexInfo(), getExportedKeys(), getImportedKeys, getTypes(), etc Says something that tables like sysodbctypes, sysodbcindexes, sysodbcforeignkeys are missing.
    Any advice?
    Thanks

    Hello,
    Which transaction/report are you facing this issue?
    Regards,
    Eduardo Rezende

  • DatabaseMetaData memory leak

    Oracle 10g.
    We're iterating over all tables (3,000+) to get dependencies. When call against DatabaseMetaData is executed, memory allocation of strings object by meta data object keeps growing:
    ResultSet rs2 = dbMetaData.getImportedKeys(catalog, null, tableName);
    It ends up allocating 600Mb+ and results in OOM exception.
    I tried closing statements in addition to result set after each iteration, but it didn't help:
    Statement s = rs2.getStatement();
    rs2.close();
    if (s != null) {
    s.close();
    Anybody has other ideas or saw anything similar?
    Thank you,
    Noel.

    It actually goes up quickly and then levels out and stays there until done (out of 30 sec of iteration processing, it jumps in first 2 seconds, grows slowly in next 5 seconds and then stays for the rest of the time). I tried to force garbage collection from profiler but it didn't have effect on total allocation.

  • Oracle JDBC Driver 10.2.0.1.0 DataBaseMetaData Incorrect For Unary FKs

    Friends,
    My firm (which is not a MetaLink subscriber) has discovered a bug in the Oracle 10g JDBC Driver (10.2.0.1.0). The bug affects unary associations, namely cases in which the foreign key of a table points to that very table's primary key.
    In such cases, method DatabaseMetaData.getExportedKeys (which is designed to return all foreign keys that reference a specified table) fails to return the foreign key reference. The method works correctly as long as the foreign key "points to" a different table. Example:
    ResultSet lResultSetForeignKeys = aDatabaseMetaData.getExportedKeys(<foo>)
    while (lResultSetForeignKeys.next())
    // NOTHING RETURNED
    In this case, the ResultSet contains no elements.
    We discovered this problem testing the same logic against a cross-section of commercial databases. All other DBMS drivers produce the correct result except for the Oracle drivers.
    If some brave soul would convert this into a MetaLink TAR we'd sure appreaciate it, and if possible we'd love to be in the loop regarding the progress on this issue. My e-mail ID is [email protected], and I'm working on behalf of Corticon Technologies, Inc.
    Sincerely,
    David Lynch
    General Manager, SOTA

    My understanding is that to be a MetaLink subscriber you have to be an Oracle customer or subscribe for Oracle support services. Since Oracle is only one of the many databases we support, and because our sole objective with Oracle is compatibility testing, it's unlikely that we will become subscribers unless it's essentially free.
    Oracle is tangential (not core) to our own offerings, so we don't have a pressing need for support. Is there some avenue for people in our shoes?

  • DatabaseMetadata.getProcedure - MS SQL REMARK location?

    Hey all,
    Using the DatabaseMetaData.getProcedures() method to retrieve sprocs. I would like very much to use the procedure REMARK column, but I do not understand where to place this information in MS SQL 2005 for each sproc. Does anyone know please?
    I saw some documentation in DB2 for a comment field, which I assume would return in the procedure REMARK field. If someone could verify DB2 location, as well as any other databases (Oracle, MySQL, Postgres, etc). MS SQL 2005 is my primary focus however.
    edit: prefer to use JTDS (1.2.2 or higher), but if can also/only be accomplished with SQLJDBC.jar, please identify.
    Edited by: binarymonk_01 on Jul 9, 2008 6:16 AM

    Also If I change to NAT from the public IP to the NLB IP to use either one of the phsyical IPs of the NLB cluster (192.168.0.50 or 51) it works fine when using the public IP.  So it's definatly an issue when NATing the VIP of NLB cluster.
    Chris

  • DatabaseMetaData.getProcedureColumns not returning correct type

    Here's the situation.
    I have an object type.
    CREATE TYPE ttest AS OBJECT (
    elem1 VARCHAR2(1),
    elem2 NUMBER(20),
    I have an Table Type of the Object Type.
    CREATE TYPE tabtest AS TABLE of ttest;
    I'm using the Table Type as input to a package function.
    function test (arrTabTest IN tabtest) return number;
    When using the DatabaseMetaData, the getProcedureColumns tells me that the parameter is of type "TABLE".
    How do I determine what the real Table Type is (ie. "tabtest")?
    Thanks
    Gary R. Kline
    Chief Technology Architect
    BVN Solutions, Inc.

    Hi Justin, thank you for responding.
    for both the ODBC and oo4o example I am using the SQL statement below:
    varSQL = "SELECT * FROM food"
    The food table has two filed, a main of type varchar2 and a quantity of type number.
    Both the main and the quantity are coming back to my ASP application as string type.
    Thank You for the help.
    Samir

  • Getting AUTO_INCREMENT info from DatabaseMetaData

    Hi all,
    I am experimenting with DatabaseMetaData on a MySQL database. Does anyone know how to get the information that a column of a table is autoincremented.
    The DatabaseMetaData.getColumns(...) method does not seem to return this info.
    Suppose a table created by
    CREATE TABLE book
    (book_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255))
    The DatabaseMetaData returns all information of this table except that book_id is AUTO_INCREMENT.
    Or am I missing something?
    cu
    Achim

    You can use what the DatabaseMetaData.getColumns(...) return. Because the method DatabaseMetaData.getColumns(...) return a ResultSet object.
    From the ResultSet you can know if the colunm is autoincrement or not.
    Use the ResultSet.getMetaData() method that return a ResultSetMetaData object, then use the ResultSetMetaData.isAutoIncrement(int column) method that Indicates whether the column is automatically numbered.Good luck

  • DatabaseMetaData.getAttributes throwing 'unsupported feature' exception

    Hi, maybe someone can help me with this.
    For a given schema, I'm trying to find the definition of a user-defined type. However, the getAttributes method of DatabaseMetaData throws an exception, claiming that the feature is unsupported.
    This is the code:
    Connection connection = null; ResultSet resultSet = null; try { connection = DriverManager.getConnection( URL, USERNAME, PASSWORD ); DatabaseMetaData dbMD = connection.getMetaData(); System.out.printf( "%s %d.%d%n", dbMD.getDatabaseProductName(), dbMD.getDatabaseMajorVersion(), dbMD.getDatabaseMinorVersion() ); resultSet = dbMD.getAttributes( null, null, "BATCH_T", "%" ); // this is line 32 // code to print the ResultSet } finally { if( resultSet != null ) resultSet.close(); if( connection != null ) connection.close(); }
    And this is the output:
    Oracle 10.2 Exception in thread "main" java.sql.SQLException: Unsupported feature         at oracle.jdbc.OracleDatabaseMetaData.getAttributes(OracleDatabaseMetaData.java:10980)         at udttest.Main.main(Main.java:32)
    The driver is the very latest one from the Oracle site, ojdbc6.jar, version 11.2.0.2.0. According to the JavaDocs of OracleDatabaseMetaData, the getAttributes method has been there since 9.0.2. So does this means that the method has merely been present, and always throws this exception?
    Alternatively, is there another way of programmatically retrieve information about a user-defined type?
    Thanks,
    - Peter

    Severity One wrote:
    jschell wrote:
    Severity One wrote:
    Yes, but the question was not how PL/SQL could know about how a Java object looks like, but how Java can know how a PL/SQL object looks like.You said the following.
    +"One issue we have at work is to pass to or receive from a PL/SQL stored procedure a Java object."+
    What I read from that is that you wish to get/send a "java object" to PL/SQL.
    Thus PL/SQL would in fact need to know what a "java object" is.No, you forgot to read the context: I wanted to know how Java could figure out how an Oracle user-defined type looked like, not the other way around. This business of "PL/SQL knowing about a Java object" is something you invented, that you keep harking on, and which I never said. So could you please drop it?
    I read what you posted, not your mind.
    Well, I could think of having the business logic for an application in PL/SQL, and using Java to create an API or web > > > Specifically, it's about an SMS sending application, which is interacting with just one table in Oracle and an SMS-C. Want to send an SMS? Insert it into the table. Want to receive one? Read it from the table. Any application that wants to send any number of SMS's, would therefore interact with this database, and a lot of things can be done purely in Oracle, without the need of having a separate application requiring the attention of support staff.Which I can implement completely and easily without using PL/SQL user defined types.Really? There are a lot of optional parameters when sending an SMS. It's not just sender, recipient and text content. So if I want to send, say an identical message to 10,000 recipients, or I want to send 10,000 individual messages; in both cases, with certain parameters for such things like time-outs, SMS-C port, protocol ID, data coding, delivery report options, priority, and all the other things that SMPP allows you to define, how would you pass those in a manner that doesn't lead to dozens of stored procedures, or very long parameter lists?
    Standard normal form in relational databases - an option table.
    And I doubt a valid format for delivery would work with 10,000 recipients stored in a single UDF verus a receiptant table.
    Not to mention of course that the fact that there might be one case for a UDF doesn't mean that the usage is a good idea for most applications. Or even that one application that uses a single UDF will need more than one of them.
    Restating what I said "...doesn't mean that it *normally* a good idea to use it."
    To clarify your confusion about that statement it means that there will in fact be some cases where it is acceptable but most cases it is not.
    Or do you prefer calling stored procedures with endless parameter lists? Or to have a whole bunch of insert and update statements called from Java?No idea what you are talking about. See the above example about sending SMS messages.
    Option table.
    First what you mention above is a standard idiom in all programming languages and used extensively since interactions between programming languages and relational database began.What, whole loads of stored procedures, or doing all the updates and inserts from Java? We've come to the conclusion that the inability to validate an SQL statement in Java source code is a bugger, and that it's better to do this sort of thing in a stored procedure. Also, using arrays means a significant performance improvement, and an array of objects/user-defined types is the next thing that comes to mind.
    That will not be a consideration in most applications. And for those applications where there is a proven performance bottleneck at the database layer most applications would be better served with one or more of the following
    1. Redoing the architecture/design
    2. Using a caching mechanism.
    3. Even look at redoing the requirements.
    Secondly I have no idea what you mean by "endles parameter lists". I do not have that problem. If you do then I can only suggest that you should look at problems with your design rather than than attempting to solve it with your implementation.If you have a lot of parameters, like with sending SMS messages, some of which are optional whilst others are not, and when many of these parameters are identical between different insert statements, you have two options:
    * use very long parameter lists.
    * use user-defined types; for example, an array with 'recipient + messages' types, and a single 'all other options' type.
    Or an option table.
    Perhaps not, but I didn't take off at a tangent. Anyway, one way of preventing binding is by having one of the frameworks figure out on its own what the other is doing. The whole reflection and annotation thing in Java lets you do that. Unfortunately, the getAttributes() method does not.
    First as you yourself noted it is irrelevant.No, that's something you claimed. When you ask for an example, don't brush them off as being irrelevant.
    My mistake then.
    Second dynamic interface code is NOT a mechanism that reduces coupling. The fact that it is dynamic does not alter the dependency.It reduced coupling between source code, which is what it's all about. I can always fix a library; it's a lot easier than changing dozens of applications.
    No it doesn't. Coupling is coupling. The manner of calling the methods doesn't alter the semantics that require that the method is called in the first place.
    That is similar to claiming that something like perl/javascript is less "coupled" because the language is interpreted versus C++ where the method calls are compiled. Which of course is not true.
    I can store data in a database, any database, in any number of ways. None of which would require a Oracle user defined type.True, but I'm using Oracle for more than just storing data. PL/SQL is extremely powerful for certain operations, and it'd be silly not to employ that power.Which has nothing to do with what I said.Quite frankly, I'm at a loss what point exactly you're trying to make. Passing data between Java and Oracle user-defined types and arrays is a total pain, so as a programmer I'm trying to find a way to do this in a standard way, something that can be put in a library. I agree that it a complex issue.
    It however is not something that most applications need however since most do not need UDFs.
    And so I have written such a library: small, database-agnostic (should work with any JDBC data source, although I haven't tested it yet), but with annotations to mark what UDT a class maps to, and to what field within the UDT each field of the class maps to. It's the annotation bit I was wondering whether it could be avoided. Obviously, because Oracle doesn't support the getAttributes method, it would no longer be database-agnostic. Which is not really a problem, because we only use Oracle, unless a vendor supports only some other database.
    Not sure what that means since most other databases do not support UDFs.
    That fact that a feature exists is not by itself a reason to use it. Java has 'strictfp' but I have never had a single business need that required that I use it. As such if I saw someone attempting to use it or stating that it must be used then I would insist that the provide specific details driven from the business needs that required its usage.The fact that you cannot think of a use or a business need, does not mean one does not exist. I suggest you re-read my first post, since that is not what I said.

  • HOw to get  DatabaseMetaData using JPA

    hi;
    I am using ejb3 , jpa for database connectivity .is there any way to get the metadata about the database in JPA.
    ie:- in JDBC we can use DatabaseMetaData for getting this. HOw can we get the Database Meta Data by using JPA.
    Regards
    Irshad

    In addition to Gurvinder references, go over the productCatalog.xml or customCatalog.xml file to know the properties in each item-descriptor of ProductCatalog.
    In your requirement skuid is same as repositoryId.
    If you are using in jsp then use SKULookUp droplet to get the sku repositoryItem.
    If you need it in java code then use catalogTools.findSKU(String skuid).
    Peace
    Shaik

  • Create table statement from DatabaseMetaData

    Hi Experts,
    Do you know if it's possible to retrieve a create table statement based on a DatabaseMetaData ? (without looping through columnName / Type)
    The idea is to get the DatabaseMetaData from one db server, and execute the Create Table statements on a different db server.
    One obvious solution would be to loop through each table's column name/type and construct the create table statement manually, but I'd like to know whether this can be automated.
    Thanks in advnace,
    Sid

    Bigger databases provide a way to access most schema information from the database itself. That doesn't mean that the jdbc meta information is sufficient nor the best way to do that.
    There are existing tools that allow for migrations as well. Especially if the migration is a one to one mapping.

  • DatabaseMetaData.getTypeInfo ()=NULL when use JDBC Driver 9.2.0.5

    Why DatabaseMetaData.getTypeInfo () is NULL when use JDBC Driver 9.2.0.5 to connect Oracle 9.0.1.1.1 database?
    In the following links
    http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.htm#02_02
    the segment "Which JDBC drivers support which versions of Oracle Database?" tells us that JDBC Driver 9.2.0.5 can support Oracle 9.0.1.1.1 database.
    thanks!

    ERROR --- java.sql.SQLException: ORA-01722: &#26080;&#25928;&#25968;&#23383;

Maybe you are looking for