I'm passing there resultsset object into the todocument(results,out) method and referring to it in the todocument method as "rs"...here are the classes and methods....(I took out the formatting to try to make it more readable).
Just to give you a little more detail....I'm running a query based on an ID..passing the results to the todocument method to create a XML representation of the resultset....which is stored in the variable "doc" and returned to the calling program...
public class printreport extends HttpServlet
{
private DataSource dataSource;
public void init(ServletConfig config) throws ServletException{
try{
Context init = new InitialContext();
dataSource = (DataSource) init.lookup("java:comp/env/jdbc/lawcom");
}catch (NamingException ex){
throw new ServletException("Cannot retrieve
java:comp/env/jdbc/conversion",ex);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection con = null;
Document doc = null;
xmlutils prt = new xmlutils();
out.println("<html><head><Title>Print report</title></head><body>");
try{
synchronized(dataSource){
con = dataSource.getConnection();
}
PreparedStatement pstmt=con.prepareStatement("Select IRLocation, IRFromDate, IRToDate, IRToTime,IRWeaponDesc, IRStranger from IRLocationData where ID<9");
ResultSet results = pstmt.executeQuery();
ResultSet results = stmt.executeQuery(getLocation);
if(!results.next()){
throw new SQLException("Missing Row");}
doc = prt.toDocument(results, out);
out.println(prt.serialize(doc));
....
}
public class xmlutils
{
public static Document toDocument(ResultSet rs, PrintWriter out) throws ParserConfigurationException, SQLException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element results =doc.createElement("Results");
doc.appendChild(results);
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
out.println(colCount);
while(rs.next())
{out.println("one record");
Element row = doc.createElement("Row");
results.appendChild(row);
for (int i=0; i <= colCount; i++)
{out.println("two record");
String columnName = rsmd.getColumnName(i);
Object value = rs.getObject(i);
Element node = doc.createElement(columnName);
node.appendChild(doc.createTextNode(value.toString()));
row.appendChild(node);
}
}
return doc;
.....
}