Question

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.

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top