I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser......
my code is as follows:
Here I am able to create a xml file ... but i need to use some array ... to store large number of data... which i am not able to do.....
could u help me regarding this,
once i do tat ... i need to display the content of the xml file in the browser.
regards,
reebeca
my code is as follows:
Code:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
// List dataList = new ArrayList();
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String contextPath = request.getContextPath();
HttpSession session = request.getSession(true);
String branchId = null;
String orgId = null;
String stateId = null;
String regionId = null;
String lot_ref="";
String lot_wt="";
String item="";
PrintWriter out = response.getWriter();
//Establish connection to MySQL database
String url="jdbc:mysql://192.168.1.3:3306/APMC?user=apmc&password=apmc";
Connection con = null;
ResultSet rs=null;
Statement st=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("driver regristration success!");
con=DriverManager.getConnection(url);
System.out.println("connected tendring............ ");
String sql = "select SQL_CALC_FOUND_ROWS lot.n_lot_ref_no,bidact.c_item_name,"+
" bidact.m_lot_wieght FROM t_bidding_activation bidact, t_lot_master lot, t_stakeholder_master stakeholder"+
" where bidact.c_active='Y' and "+
"bidact.n_lot_id=lot.n_lot_id and stakeholder.n_stakeholder_id=lot.n_stakeholder_id and bidact.c_bidded!='Y'"+
"and bidact.c_branch_id='" + branchId + "' and bidact.c_org_id='"+ orgId +"' and bidact.c_state_id='" + stateId + "' "+
"and bidact.c_region_id='" + regionId + "' order by bidact.c_item_name,"+
"bidact.d_bid_end_date, bidact.d_bid_end_time";
System.out.println("Output from dopost..........................");
System.out.println("after query..................................");
st = con.createStatement();
st.executeQuery (sql);
rs = st.getResultSet();
System.out.println("result" +rs);
System.out.print("before while");
while (rs.next()){
//Add records into data list
System.out.println("Hello....................................");
lot_ref= rs.getString(1);
System.out.print("refnum"+lot_ref);
lot_wt=rs.getString(2);
System.out.print("lotwt"+lot_wt);
item= rs.getString(3);
System.out.print("item"+item);
// dataList.add(rs.getString("lot.n_lot_ref_no"));
// dataList.add(rs.getString("bidact.c_item_name"));
// dataList.add(rs.getString("bidact.m_lot_wieght"));
}
System.out.print("after while");
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
/*if(request.getParameter("strUserName")!=null && request.getParameter("strPassword")!=null)
{
String username = request.getParameter("strUserName").toString();
String passwd = request.getParameter("strPassword").toString();
}*/
// if (uName.equals(userName) && passwd.equals(password)){
try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
Document doc = docBuilder.newDocument();
new xmlServlet().createXmlTree(doc,lot_ref, lot_wt,item);
pw.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}
try{
rs.close ();
st.close ();
con.close();
}catch(Exception e){
e.printStackTrace();
}
/* }
else
{
System.out.println("Invalid User");
}*/
}
public void createXmlTree(Document doc, String lot_ref, String lot_wt, String item) throws Exception {
//This method creates an element node
Element root = doc.createElement("User");
//adding a node after the last child node of the specified node.
doc.appendChild(root);
Element child1 = doc.createElement("lot_ref");
root.appendChild(child1);
Text text = doc.createTextNode(lot_ref);
child1.appendChild(text);
Element element = doc.createElement("lot_wt");
root.appendChild(element);
Text text1 = doc.createTextNode(lot_wt);
element.appendChild(text1);
Element element1 = doc.createElement("item");
root.appendChild(element1);
Text text2 = doc.createTextNode(item);
element.appendChild(text2);
//TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("c:/temp/tender.xml");
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();
}
}
could u help me regarding this,
once i do tat ... i need to display the content of the xml file in the browser.
regards,
reebeca
Comment