4

I have two threads. One is extracting data from database and another is displaying progressbar with abort button. If I press abort button, the query getting executed by another thread should get cancelled.

I know how to kill it from command prompt; but, if anyone of you is knowing about cancelling query from java, that will be help for me.

Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
  • http://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java – Frederic Close Dec 09 '13 at 09:33
  • 1
    @Frederic.. This will just stop the thread which was created by me. It wont stop mysql command running on mysql server.. –  Dec 09 '13 at 10:36

1 Answers1

3

Here are some pointers, that could help:

You can call Statement.cancel from another Thread to stop that statement

From the javadoc http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#cancel()

Cancels this Statement object if both the DBMS and driver support aborting an SQL statement. This method can be used by one thread to cancel a statement that is being executed by another thread.

Statement.cancel is supported by mysql driver since version 5.0

EDIT

You can also specify the maximum time a query can take by using Statement.setQueryTimeout(int seconds)

http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#setQueryTimeout(int)

Frederic Close
  • 9,389
  • 6
  • 56
  • 67