Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete all records from database

 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using xampp as local server to run my database
on clicking the button reset all records from the table sales should have been deleted instead
it is giving this error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM sales' at line 1

code part:

 
Tim Cooke
Sheriff
Pie
Posts: 3204
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Column definitions are invalid for a delete statement, so the * is invalid. Try this:
 
Tim Cooke
Sheriff
Pie
Posts: 3204
142
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, you have a potential resource leak in your code:

If an exception is thrown by a call to stmt or con then neither of those resources get closed. It's recommended to put the close() statements inside a finally block so they get called whatever happens. Like this:
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have try TRUNCATE TABLE
it works (y)
 
Brian Tkatch
Bartender
Posts: 567
25
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vani Sweety wrote:i have try TRUNCATE TABLE it works (y)


There are differences between TRUNCATE and DELETE. You probably want TRUNCATE, just beware of the differences.
 
Tim Cooke
Sheriff
Pie
Posts: 3204
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,
is equivalent to

Glad you got it sorted.

Edit: In light of Brian's post, I'll clarify that equivalence isn't the same as equality. Read up on the practical differences and make your choice accordingly.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:If an exception is thrown by a call to stmt or con then neither of those resources get closed. It's recommended to put the close() statements inside a finally block so they get called whatever happens. Like this:

And if you are using Java 7 (or later), you should use a try-with-resources and benefit from the automatic resource management (ARM). And if you are using a JDBC 4.0 driver, you no longer need to explicitly load JDBC drivers using Class.forName(). So the code snippet can be simplified toLooks pretty neat, doesn't it?

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic