Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
| Search | Today's Posts | Mark Forums Read
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 1st, 2005, 08:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Arlington, TX, USA.
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default Newbie question: check if table exists in DB

I am trying to check if a table exists in the DB before dropping the table in the following method that I have put together in a JUnit test trying to use the getMetaData() from the API:

    public void testDropTable () throws SQLException{
        Connection con = getConnection();
        Statement stmt = con.createStatement();
        ResultSet checkTable = con.getMetaData().getTables(null, null, "POITest", null);
        if (checkTable != null){
            try {
                String dropTable = "DROP TABLE ";
                String[] tables = DB_TABLE;
                for (int i = 0; i < tables.length; i++){
                    String stringCode = new String();
                    stringCode = stringCode + tables[i];
                    System.out.println(dropTable + tables[i]);

                    // Drop each table in the array.
                    int temp = stmt.executeUpdate(dropTable + tables[i]);
                }
            }
            catch (Exception e) {
                fail("Drop Table testDropTable threw an exception: " +(e.getMessage()));
            }
        }
        else{
            con.close();
        }
    }

If my table does not exist it still goes into the IF statement and I just want to bypass this method all together. Any suggestions or direction would be appreciated.

Thanks.
  #2 (permalink)  
Old August 2nd, 2005, 02:01 AM
Authorized User
 
Join Date: Aug 2004
Location: cluj, romania, Romania.
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default


Hi,

Try in the IF statement checkTable.next()

Regards,
 LOOP

  #3 (permalink)  
Old August 2nd, 2005, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Arlington, TX, USA.
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for the response. The bolded code is what I found to fix my problem and I hope it helps someone else:

public void testDropTable () throws SQLException{
        Connection con = getConnection();
        Statement stmt = con.createStatement();
        ResultSet checkTable = con.getMetaData().getTables(null, null, "POI", null);
        String tableName = null;
        while (checkTable.next())
        {
            System.out.println("In here");
            tableName = checkTable.getString("TABLE_NAME");
            System.out.println(tableName);
        }
        if (tableName != null){
            try {
                String dropTable = "DROP TABLE ";
                String[] tables = DB_TABLE;
                for (int i = 0; i < tables.length; i++){
                    String stringCode = new String();
                    stringCode = stringCode + tables[i];
                    System.out.println(dropTable + tables[i]);

                    // Drop each table in the array.
                    int temp = stmt.executeUpdate(dropTable + tables[i]);
                }
            }

            catch (Exception e) {
                fail("Drop Table testDropTable threw an exception: " +(e.getMessage()));
            }
        }
        else{
            con.close();
        }
    }

Thank you.
The Following User Says Thank You to savoym For This Useful Post:
v4victor4u (February 15th, 2012)
  #4 (permalink)  
Old February 15th, 2012, 02:19 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2012
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Friend of Wrox

I also want this code for my application.
I have one table "data_table" in "local_data" Microsoft Access database file.
I want my application first check whether this table exit or not. If exit then delete it, if not exit do nothing.
So what I need to change in your code.
In your code:
Code:
  tableName = checkTable.getString("TABLE_NAME");
here I need to change "TABLE_NAME" with "data_table"?
And what I need to change here:
Code:
String[] tables = DB_TABLE;
or I also need to change in other part of program.
  #5 (permalink)  
Old March 9th, 2012, 06:57 AM
Registered User
Points: 6, Level: 1
Points: 6, Level: 1 Points: 6, Level: 1 Points: 6, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hi

Hello, I am new to the forum.


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to check the username already exists in table asad_black ASP.NET 2.0 Basics 2 September 15th, 2008 07:19 AM
check whether COM object exists EddyT C# 0 June 15th, 2007 04:10 AM
check whether a particular session exists or not chayanvinayak PHP How-To 1 May 1st, 2006 04:09 PM
check if a column exists in the table sands SQL Server 2000 2 April 21st, 2006 11:08 PM
Append table question - newbie at work Trisomy Access 1 December 4th, 2005 11:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.