Using the Tools | Generate Code… selection from the menu will guide you through a wizard that will create the skeleton of code you need for the outputs.
As you don’t know how long the number of items are, you can use java.util.ArrayList or java.util.Vector to build up your results. Then convert them to an array when you’re done.
Another approach is to divide the reading of the data from the formatting. Use JDBC Adapter service(s) to select the data you need, then pass the results to the service that puts the data into the document format you need.
Are you sure you need to do this as a Java service? This type of operation is exactly what IS is designed for and can usually be easily accomplished without dropping to Java.