How to make writer or calc handle external data automatically?

Is it possible to have a program send parameters to base and have it query mysql DB then output the query information in writer or calc? I am trying to report numbers from a program into different templates I made in base. Depending on what number I get from the program, let said 1, I want to use load template A, if I get 2, I want to load template B. It also need to fetch relevant info from mySQL db depending on what template it is to populate the template. I can program in JAVA so I am tying to get an idea if this is possible at all. If so, what would I need to learn to do this? Any reference to document or similar examples would be great (even pieces of it). Thank you.

@Question2 Please explain what you mean by a template in Base as this is unfamiliar there. BTW - based upon your immediate description, it is uncertain as to the need for Base at all.

If this question is no longer relevant (as you have stated similar without Base in another question) please close this question. What you should have done was simply edit this question.

Near duplicate question is Pass information to writer / calc as it opens