Detect if table exists

Discussion in 'Java' started by raffaele castagno, Aug 8, 2004.

  1. Il Sun, 08 Aug 2004 23:46:11 +0530, Manish Hatwalne ha scritto:

    > This should be easy, but I am unable to find it now.
    > How can I detect if a particular table exists or not in my DB (I am using
    > MySQL).
    > I cannot delete table, so I can't use drop if exists; but can I use count
    > (*) if exists, or sth similar? I am not much of a DB person.
    > Any pointers would be very useful.


    AFAIK, if you try to access a table wich does not exist, JDBS should raise
    a DataAccessException, if I remember right... You can try to run a query
    on the table you want to check, and catch that exception.

    Read MySql JDBC driver and Java JDBC documentation for details, and of
    course wait the answer of developer more experienced than me...

    Bye

    Raffaele
    raffaele castagno, Aug 8, 2004
    #1
    1. Advertising

  2. This should be easy, but I am unable to find it now.
    How can I detect if a particular table exists or not in my DB (I am using
    MySQL).
    I cannot delete table, so I can't use drop if exists; but can I use count
    (*) if exists, or sth similar? I am not much of a DB person.
    Any pointers would be very useful.

    TIA,
    - Manish
    Manish Hatwalne, Aug 8, 2004
    #2
    1. Advertising

  3. raffaele castagno

    sks Guest

    "Manish Hatwalne" <> wrote in message
    news:...
    > This should be easy, but I am unable to find it now.
    > How can I detect if a particular table exists or not in my DB (I am using
    > MySQL).
    > I cannot delete table, so I can't use drop if exists; but can I use count
    > (*) if exists, or sth similar? I am not much of a DB person.
    > Any pointers would be very useful.


    You could use DESCRIBE tablename and catch the exception if it doesn't
    exist.

    Or you could use SHOW TABLES and loop through results to see if its exists.
    sks, Aug 9, 2004
    #3
  4. "Manish Hatwalne" <> writes:

    > How can I detect if a particular table exists or not in my DB (I am using
    > MySQL).


    Ask the driver for database metadata, and check for the table there.

    Connection c = ...
    DatabaseMetaData dbm = c.getMetaData();
    ResultSet tables = dbm.getTableNames(null, null, "TABLENAME", null);
    if (rs.next()) {
    // Table exists
    }
    else {
    // Table does not exist
    }

    Adjust parameters, check for multiple entries etc. as you see fit.
    Tor Iver Wilhelmsen, Aug 9, 2004
    #4
  5. raffaele castagno

    Liz Guest

    "Tor Iver Wilhelmsen" <> wrote in message
    news:...
    > "Manish Hatwalne" <> writes:
    >
    > > How can I detect if a particular table exists or not in my DB (I am

    using
    > > MySQL).

    >
    > Ask the driver for database metadata, and check for the table there.
    >
    > Connection c = ...
    > DatabaseMetaData dbm = c.getMetaData();
    > ResultSet tables = dbm.getTableNames(null, null, "TABLENAME", null);
    > if (rs.next()) {
    > // Table exists
    > }
    > else {
    > // Table does not exist
    > }
    >
    > Adjust parameters, check for multiple entries etc. as you see fit.


    I use
    execute("SHOW TABLES");
    which returns a result set that contains the names of all tables in
    the database.
    This way you have no exception to worry about.
    Liz, Aug 9, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Totan
    Replies:
    0
    Views:
    917
    Totan
    Apr 17, 2006
  2. Kevin Smith
    Replies:
    3
    Views:
    359
  3. Ulf Meinhardt
    Replies:
    8
    Views:
    6,113
  4. Qi
    Replies:
    1
    Views:
    271
    Cholo Lennon
    Jul 1, 2011
  5. Chuck Remes

    [Q] how to detect if #super exists?

    Chuck Remes, Oct 27, 2008, in forum: Ruby
    Replies:
    8
    Views:
    94
    Pit Capitain
    Oct 27, 2008
Loading...

Share This Page