Disallowing MariaDB query cache except from webserver (JDBC)
If there is a lot of write activity, the Query Cache is virtually useless -- because all QC entries for a table are purged when any modification happens to the table.

If the QC is useful, using query_cache_type=DEMAND and manually saying SQL_CACHE or SQL_NO_CACHE on every SELECT may be optimal.  This keeps the rare, bulky, queries from cluttering the cache, while allowing the 'good' ones to make use of the QC.  Note:  This has nothing to do with the connector (JDBC, etc), only with the SELECT statements.

Yes, DEMAND without SQL_CACHE leads to not using the QC.  I agree that this is "not ideal" since it depends on un-obvious rules about the QC.  Still, it may be your best solution, other than...

If a query is slow slow that it matters, we should look critically at the query.  The speed-up may be as simple as adding a 'composite' index, or some seemingly minor tweak to the query such as not hiding an indexed column in a function call (eg, DATE(dt) = ...).