public static void printTrace(
      java.sql.SQLException e,
      java.io.PrintWriter printWriter,
      String messageHeader,
      boolean returnTokensOnly) {
    String header;
    synchronized (printWriter) {
      while (e != null) {
        if (e instanceof java.sql.DataTruncation) {
          header =
              messageHeader + "[" + "[email protected]" + Integer.toHexString(e.hashCode()) + "]";
          printWriter.println(header + " java.sql.DataTruncation");
        } else if (e instanceof java.sql.SQLWarning) {
          header = messageHeader + "[" + "[email protected]" + Integer.toHexString(e.hashCode()) + "]";
          printWriter.println(header + " java.sql.SQLWarning");
        } else if (e instanceof java.sql.BatchUpdateException) {
          header =
              messageHeader
                  + "["
                  + "[email protected]"
                  + Integer.toHexString(e.hashCode())
                  + "]";
          printWriter.println(header + " java.sql.BatchUpdateException");
        } else { // e instanceof java.sql.SQLException
          header = messageHeader + "[" + "[email protected]" + Integer.toHexString(e.hashCode()) + "]";
          printWriter.println(header + " java.sql.SQLException");
        }

        printWriter.println(header + " SQL state  = " + e.getSQLState());
        printWriter.println(header + " Error code = " + String.valueOf(e.getErrorCode()));
        printWriter.println(header + " Message    = " + e.getMessage());

        if (e instanceof java.sql.DataTruncation) {
          printWriter.println(
              header + " Index         = " + ((java.sql.DataTruncation) e).getIndex());
          printWriter.println(
              header + " Parameter     = " + ((java.sql.DataTruncation) e).getParameter());
          printWriter.println(
              header + " Read          = " + ((java.sql.DataTruncation) e).getRead());
          printWriter.println(
              header + " Data size     = " + ((java.sql.DataTruncation) e).getDataSize());
          printWriter.println(
              header + " Transfer size = " + ((java.sql.DataTruncation) e).getTransferSize());
        }

        if (e instanceof java.sql.BatchUpdateException) {
          printWriter.println(
              header
                  + " Update counts = "
                  + Utils.getStringFromInts(((java.sql.BatchUpdateException) e).getUpdateCounts()));
        }

        printWriter.println(header + " Stack trace follows");
        e.printStackTrace(printWriter);

        e = e.getNextException();
      }

      printWriter.flush();
    }
  }