code: public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String csvOptions) throws SQLException, IOException {
          String regex = ".*(?i)\\b(select|from)\\b.*";
          Pattern pattern = Pattern.compile(regex);
          Matcher matcher = pattern.matcher(tableReference);
          if (matcher.find()) {
              if (tableReference.startsWith("(") && tableReference.endsWith(")")) {
                  try (Statement st = connection.createStatement()) {
                      JDBCUtilities.attachCancelResultSet(st, progress);
                      Csv csv = new Csv();
                      if (csvOptions != null && csvOptions.indexOf('=') >= 0) {
                          csv.setOptions(csvOptions);
                      }
                      csv.write(fileName.getPath(), st.executeQuery(tableReference), null);
                  }
              } else {
                  throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'.");
              }
  
          } else {
              if (FileUtil.isExtensionWellFormated(fileName, "csv")) {
                  final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData());
                  TableLocation location = TableLocation.parse(tableReference, isH2);
                  try (Statement st = connection.createStatement()) {
                      JDBCUtilities.attachCancelResultSet(st, progress);
                      Csv csv = new Csv();
                      if (csvOptions != null && csvOptions.indexOf('=') >= 0) {
                          csv.setOptions(csvOptions);
                      }
                      csv.write(fileName.getPath(), st.executeQuery("SELECT * FROM " + location.toString()), null);
                  }
              } else {
                  throw new SQLException("Only .csv extension is supported");
              }
          }
      }
docs: Export a table or a query to a CSV file
  
  @param connection Active connection, do not close this connection.
  @param tableReference [[catalog.]schema.]table reference
  @param fileName File path to read
  @param progress
  @param csvOptions  the CSV options ie "charset=UTF-8 fieldSeparator=| fieldDelimiter=,"
  @throws SQLException
  @throws IOException