java.sql.*

i need to delete all information in my table.  how do i go about doing that?

stmt.executeUpdate("DELETE * FROM TableName");  ???
capnjazzAsked:
Who is Participating?
 
rkrenekConnect With a Mentor Commented:
capnjazz,
You're close. Try:
stmt.executeUpdate("DELETE FROM TableName");
then you can do:
int updates = stmt.getUpdateCount();
to get the number deleted.
If you want to delete specific rows you would add a 'WHERE' clause to the end of your sql statement like:
stmt.executeUpdate("DELETE FROM TableName WHERE column1 = 'something'");

rkrenek
0
 
sankars98Commented:

 yes you are correct and after that you have to
 call "commit()" of Connection object.
0
 
capnjazzAuthor Commented:
well i had just "DELETE FROM TableName", but it threw an exception?
0
 
capnjazzAuthor Commented:
System.out.println("Transmitter");
stmt.executeUpdate("DELETE FROM Transmitter");
System.out.println("Deleted Transmitter");

stmt.executeUpdate("INSERT INTO Transmitter VALUES (10, 'Washington Avenue',  'transmitting routine messages', '9,301-464-6824', '12345', '', 'I-279', 'Allegheny', '11', 'dxxxB1C1', '')");
stmt.executeUpdate("INSERT INTO Transmitter VALUES (20, 'Carson Street',      'transmitting routine messages', '9,301-464-3756', '98765', '', 'I-279', 'Allegheny', '11', 'dxxxB1C1', '')");
stmt.executeUpdate("INSERT INTO Transmitter VALUES (40, 'Main Street',        'failed',                        '9,410-922-0090', '68734', 'Transmitter fell down when bionic beaver thought pole was a tree and chewed through it.', 'I-79', 'Butler', '12', 'dxxxB1C2', '07/04/99 08:30')");

i don't get to "Deleted Transmtter".
i checked and i do have one entry in the table.
0
 
rkrenekCommented:
try:
try {
      boolean status = stmt.execute("DELETE FROM             transmitter");
      if (!status) {
            System.out.println("Deleted Transmitter");
      }
} catch (SQLException e)  {
      System.err.println("SQLExcepton: " + e.getMessage() + ":" +e.getSQLState());
}      

The sql above is correct.
Above will do an update or query, if it was an update it will print "Deleted Transmitter". If there is an error it will print it out. Some problems might be the user that is running this may not be able to delete records from that table. Hope this gets you closer to your problem.

rkrenek
0
All Courses

From novice to tech pro — start learning today.