Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #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.
Reply With Quote
  #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

Reply With Quote
  #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.
Reply With Quote
The Following User Says Thank You to savoym For This Useful Post:
v4victor4u (February 15th, 2012)
  #4 (permalink)  
Old February 15th, 2012, 01: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.
Reply With Quote
  #5 (permalink)  
Old March 9th, 2012, 05: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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 10:07 AM



All times are GMT -4. The time now is 02:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.