If you truly mean any table, I really don't believe this can be done in standard SQL. If you have an either/or situation, you might be able to craft a solution using outer joins.
It can however be done in many stored-procedure languages; just how depends on the stored procedure language your database supports, if any. (Stored procedures are called from JDBC using CallableStatement objects.)
Generally, such "dynamic" approaches can lead to various types of problems however; it often makes it tough to use PreparedStatement (or the appropriate parallel technique in the stored procedure language), and it often becomes hard to enforce referential integrity in the database.