1

I have written this code inside a servlet to delete certain records from three tables.

Note: two of these tables are table for Full-text search reason and other one is table.

int id = Integer.parseInt(request.getParameter("id"));
try {
  Statement stmt = conn.createStatement();
  stmt.executeUpdate("DELETE FROM proposal, suitable_for,skill_need WHERE proposal_id="
                     +id);

I get the following error. I cant see any syntax error in the query I have write

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
  You have an error in your SQL syntax; 
  check the manual that corresponds to your MySQL server version for the 
  right syntax to use near 'where proposal_id=12' at line 1
at sun.reflect.GeneratedConstructorAccessor14.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1748)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1665)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at Lucene.Core.indexer.Action.doDelete(Action.java:184)
at Lucene.Core.indexer.Action.doGet(Action.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
3
  • 1
    does this proposal_id exist in all three of the tables: proposal, suitable_for, skill_need Commented Jul 6, 2012 at 12:33
  • yes it dose exist in all of them Commented Jul 6, 2012 at 12:34
  • 1
    And the query works from the client command line? Commented Jul 6, 2012 at 12:35

4 Answers 4

1

Delete from can delete data from table like this but i don't know whether if can delete data like you post.

"DELETE  FROM proposal WHERE    proposal_id="+id
"DELETE  FROM suitable_fo WHERE    proposal_id="+id
"DELETE  FROM skill_need WHERE    proposal_id="+id

And before you invoke this, make sure param id is being checked to avoid sql injection.

Sign up to request clarification or add additional context in comments.

2 Comments

what is the professional way for checking param before using it inside query due to avoiding mysql injection.
Thats not so easy, to your situation if id is integer. Make sure is integer before you execute the sql.
0

Have you tried deleting from the three tables separately (possibly inside a transaction?)

Delete from multiple tables requires a table reference (e.g. a JOIN). It was born to delete linked records.

So you should try

 DELETE FROM proposal, suitable_for, skill_need
 FROM proposal JOIN suitable_for ON (proposal.proposal.id = suitable_for.proposal_id)
 JOIN skill_need ON (skill_need.proposal.id = proposal.proposal.id)
 WHERE proposal.proposal_id = ...

Comments

0

Try using executeQuery instead of executeUpdate.

Comments

0

I don't have a mySql db to test it, but I think the problem might be that it cannot determine which proposal_id it should use (having 3 tables). have you tried expliciting the join and specifying a table name before proposal_id?

something like "DELETE FROM proposal, suitable_for,skill_need WHERE proposal.proposal_id= suitable_for.proposal.proposal_id AND proposal.proposal_id=skill_need.proposal_id and proposal.proposal_id="+id

still, I agree with the others, 3 separate delete would be a better option..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.