code: public static void printStackTrace(SQLException e, PrintWriter pw) {
  
          SQLException next = e;
          while (next != null) {
              next.printStackTrace(pw);
              next = next.getNextException();
              if (next != null) {
                  pw.println("Next SQLException:");
              }
          }
      }
docs: Print the stack trace for a SQLException to a
  specified PrintWriter.
  
  @param e SQLException to print stack trace of
  @param pw PrintWriter to print to