The cancel() method cancels currently executing queries. The cancel request is sent from the client to the server. In some cases, the server might not cancel the query. For example, the server will not cancel the query if the query is not cancellable or if the query has already completed.
The COPY command is not cancellable.
For detailed documentation on the cancel() method, see the Java documentation on this web site:
http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#cancel%28%29
The following is a code example that uses the cancel() method:
// This demonstrates how to run a query in one thread and cancel it in // a different thread. // // You will need to customize the IP address of the queen in the variable // "DB_URL" later in this code. // You'll also need to add a table named "demo1", or modify the SQL statement // to use a table that you already have. import java.sql.*; // This class allows us to run a query in a separate thread from the main // thread (the main thread will cancel this query). class MyThread extends Thread { Statement myStatement; // The SQL statement that we want to execute. String mySql = ""; MyThread(Statement myStmt, String pSql) { myStatement = myStmt; mySql = pSql; } public void run(){ try { System.out.println("Running query in different thread"); myStatement.executeQuery(mySql); } catch(Exception e) { e.printStackTrace(); } } } public class CancelQuery { static final String JDBC_DRIVER = "com.asterdata.ncluster.Driver"; // Customize this URL to access your own server! static final String DB_URL = "jdbc:ncluster://10.80.169.10:2406/beehive"; public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null; try{ // Register JDBC driver Class.forName(JDBC_DRIVER); // Open a connection to the database server. System.out.println("Connecting to database..."); // Customize the database user and password if you wish. conn = DriverManager.getConnection(DB_URL,"beehive","beehive"); // Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); // If the table "demo1" is a large table, this statement might run long // enough that it will be practical to cancel it. String sql = "SELECT COUNT(*) FROM demo1;"; ResultSet rs = null; // Run the query in a separate thread. MyThread thread = new MyThread(stmt, sql); thread.start(); Thread.sleep(5000); // 5,000 milliseconds, in this case System.out.println("Invoking cancel"); stmt.cancel(); // Wait until the thread running the query finishes. thread.join(); // Clean up. stmt.close(); conn.close(); } catch(SQLException se) { //Handle errors for JDBC se.printStackTrace(); } catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); } finally{ //finally block used to close resources try{ if (stmt!=null) stmt.close(); } catch(SQLException se2){ }// nothing we can do try{ if(conn!=null) conn.close(); } catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); } }