import java.sql.*;

public class JdbcCancel {
	static Statement stmt = null;
	
	public static void main(String[] args) {
		String     query = "SELECT * FROM diag";
		String	   url = "jdbc:mysql://localhost/connect?user=root&useSSL=false";
		Connection conn = null;
	    ResultSet  rs = null;
	    
	    if (args.length > 0)
	        url = args[0];
	    
	    if (args.length > 1)
	        query = args[1];
	    
		try {
			conn = DriverManager.getConnection(url);
			System.out.println("Connection " + conn.toString() + " established");
		    stmt = conn.createStatement();
		    stmt.setFetchSize(Integer.MIN_VALUE);
		    rs = stmt.executeQuery(query);
		    
		    // Display the result
		    PrintResult(rs);
		} catch (SQLException se) {
			System.out.println("SQL Exception:") ;

			// Loop through the SQL Exceptions
			while (se != null) {
				System.out.println("State  : " + se.getSQLState());
			    System.out.println("Message: " + se.getMessage());
			    System.out.println("Error  : " + se.getErrorCode());

			    se = se.getNextException();
			} // end while se
			
		} catch( Exception e ) {
			System.out.println(e);
		} finally {
		    //finally block used to close resources
		    if (rs != null)
			    try {
				    if (!rs.isAfterLast()) {
//				    	CancelThread c = new CancelThread();
//				    	c.start();
//				    	Thread.sleep(100);
						System.out.println("Canceling query");
				    	stmt.cancel();
						System.out.println("Closing statement");
						stmt.close();
						stmt = null;
						System.out.println("Statement closed");
				    } else
				    	rs.close();
				    
				} catch(SQLFeatureNotSupportedException fe) {
					fe.getMessage();
			    } catch(SQLException re) {
			    	re.printStackTrace();
//			    } catch(InterruptedException ie) {
//					ie.getMessage();
			    } // nothing else we can do
		      
		    if (stmt != null)
			    try {
		            stmt.close();
			    } catch(SQLException se) {
			    	se.printStackTrace();
			    } // nothing else we can do
		      
	        if (conn != null)
	        	try {
		            conn.close();
	        	} catch (SQLException ce) {
	        		ce.printStackTrace();
	        	} // nothing else we can do
		    
		} // end try/catch/finally
		
	} // end of main
	
	private static void PrintResult(ResultSet rs) throws SQLException {
    	// Get result set meta data
    	ResultSetMetaData rsmd = rs.getMetaData();
    	int i, n = 0;
    	int ncol = rsmd.getColumnCount();

    	// Get the column names; column indices start from 1
    	for (i = 1; i <= ncol; i++) {
    		String columnName = rsmd.getColumnName(i);

    		// Get the name of the column's table name
    		//String tableName = rsmd.getTableName(i);
    		if (i > 1)
		    	System.out.print(", ");
    		
    		System.out.print(columnName);
    	} // endfor i
    	
    	System.out.println();
	    
	    // Loop through the result set
	    while (rs.next()) {
			if (rs.getString(3).equals("-"))
	    		break;
	    	
	    	for (i = 1; i <= ncol; i++) {
	    		if (i > 1)
			    	System.out.print(", ");
	    		
	    		try {
	    			switch (rsmd.getColumnType(i)) {
	    			case java.sql.Types.VARCHAR:
	    			case java.sql.Types.CHAR:
			    		System.out.print(rs.getString(i));
	    				break;
	    			case java.sql.Types.INTEGER:
			    		System.out.print(rs.getInt(i));
	    				break;
	    			case java.sql.Types.SMALLINT:
			    		System.out.print(rs.getInt(i));
	    				break;
	    			default:
	    				break;
	    			} // endswitch Type
	    		
	    		} catch (SQLException se) {
		    		System.out.print(rs.getString(i));
	    		} // end try/catch
	    		
	    	} // endfor i
	    	
	    	System.out.println();
	    } // end while rs

	} // end of PrintResult
	
/*	static class CancelThread extends Thread {
		@Override
		public void run() {
			System.out.println("Canceling query");
			try {
			    stmt.cancel();
			} catch(SQLFeatureNotSupportedException fe) {
				fe.getMessage();
			} catch(SQLException se) {
			    se.printStackTrace();
			} // nothing more we can do
			
		} // end of run
		
	} // end of class Cancel */

} // end of class JdbcCancel
