import java.sql.*; public class MySQLTest { static String host = "localhost"; static String database = "test"; static String user = "scott"; static String password = "tiger"; static String connect = "jdbc:mysql://" + host + "/" + database + "?user=" + user + "&password=" + password + "&dumpQueriesOnException=true"; static String SHORT_QUERY = "SELECT SLEEP(2)"; static String LONG_QUERY = "SELECT SLEEP(2) FROM some_real_table"; public static void main(String[] args) throws Exception { Connection conn = DriverManager.getConnection(connect); run(SHORT_QUERY, 1, conn); run(LONG_QUERY, 1, conn); } public static void run(String query, int timeout, Connection conn) { PreparedStatement ps = null; System.out.println("Running query: " + query); try { ps = conn.prepareStatement(query); ps.setQueryTimeout(timeout); ps.execute(); } catch (SQLException sqle) { dumpException(sqle); } } public static void dumpException(SQLException sqle) { System.out.println("Exception type: " + sqle.getClass().getName()); System.out.println("Exception cause: " + sqle.getCause()); System.out.println("SQLState: " + sqle.getSQLState()); System.out.println("Error Code: " + sqle.getErrorCode()); System.out.print("Stace trace: "); sqle.printStackTrace(System.out); } }