Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql drop all tables in a table 1

Status
Not open for further replies.

javanemo

Technical User
Sep 9, 2003
14
0
0
AU
Hi all.. I am new to java and I am thinking of dropping all the tables in mysql.
I have written halfway and I am stuck. can someone help me?

String query = "SELECT tablename FROM tablelist";
ResultSet rs = con.runQuery(query);
ArrayList infoList = new ArrayList();
while (rs.next()) {
infoList.add(result.getString(1));
}
out.println("All tables in tablelist are removed");


below is all the list of the table names stored in tablelist
+-----------+
| tablename |
+-----------+
| newfour |
| newone |
| newthree |
| newtwo |
| newfive |
| newsix |
+-----------+

Each item is a table stored in the database and also has records in them.
 
While looping your ResultSet of table names, execute the sql command to drop the table (I think in MySQL its "drop tablename").

Or you could do it using the MySQL console ...
 
forgot to include that I wish to write a seperate method to drop the tables in sql, so that it I dun have to write multiple commands each time I wan to drop the table rite?
 
Code:
   public void dropTable(ArrayList al) {
          for (int i = 0; i < al.size(); i++) {
                 String table = (String)al.get(i);
                 // Add your JDBC code to run the drop SQL command here
          }
   }
 
I wrote the function below but I am not sure how to parse the tablenames.
Basically I am confused now. The examples from the google really messed me up.

private void dropTable() throws SQLException
{
try {
out.println(&quot;Dropping Table.............&quot;);
stmt = conn.createStatement();
String sql = &quot;DROP TABLE testTable&quot;;
int deltable = stmt.executeUpdate(sql);
out.println(&quot;OK&quot;);
}
catch(SQLException sqle) {
out.println(&quot;dropTable - Error dropping table:&quot; + sqle + sqle.getMessage());
}
catch (Exception e) {
out.println(&quot;dropTable - Error dropping table:&quot; + e + e.getMessage());
}

finally {
try {
stmt.close();
}
catch (SQLException sqle) {}
}
}
 
Try ...

Code:
   public void dropTable(ArrayList al) {
          for (int i = 0; i < al.size(); i++) {
                 String table = (String)al.get(i);
                 // Add your JDBC code to run the drop SQL command here
                 try {
                     out.println(&quot;Dropping Table.. &quot; +table);
                      stmt = conn.createStatement();
                      String sql = &quot;DROP TABLE &quot; + table;
                      int deltable = stmt.executeUpdate(sql);
                      out.println(&quot;OK - exit status &quot; +deltable);
}
                  catch(SQLException sqle) {
                    out.println(&quot;dropTable - Error dropping table:&quot; + sqle + sqle.getMessage());
                    }
                   catch (Exception e) {
                      out.println(&quot;dropTable - Error dropping table:&quot; + e + e.getMessage());
                    }

                    finally {
                       try {
                         stmt.close();
                       }
                        catch (SQLException sqle) {}
                     }
          }
   }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top