472,333 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

Store query result in a xml file

5
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:

Expand|Select|Wrap|Line Numbers
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)
  2.                                    throws ServletException,IOException{
  3. //      List dataList = new ArrayList();
  4.       response.setContentType("text/html");
  5.     PrintWriter pw = response.getWriter();
  6.  
  7.    String contextPath = request.getContextPath();
  8.    HttpSession session = request.getSession(true);
  9.  
  10.  String branchId = null;
  11.  String orgId = null;
  12.  String stateId = null;
  13.  String regionId = null;
  14.  String lot_ref="";
  15.  String lot_wt="";
  16.  String item="";
  17.  
  18.  
  19.  
  20.  
  21.   PrintWriter out = response.getWriter();
  22.     //Establish connection to MySQL database
  23.      String url="jdbc:mysql://192.168.1.3:3306/APMC?user=apmc&password=apmc";
  24.     Connection con = null;
  25.     ResultSet rs=null;
  26.     Statement st=null;
  27.     try {
  28.  
  29.  
  30.        Class.forName("com.mysql.jdbc.Driver").newInstance();
  31.        System.out.println("driver regristration success!");
  32.        con=DriverManager.getConnection(url);
  33.        System.out.println("connected tendring............     ");
  34.  
  35.       String sql = "select SQL_CALC_FOUND_ROWS lot.n_lot_ref_no,bidact.c_item_name,"+
  36.                             " bidact.m_lot_wieght FROM t_bidding_activation bidact, t_lot_master lot, t_stakeholder_master stakeholder"+
  37.                            " where bidact.c_active='Y' and "+
  38.                             "bidact.n_lot_id=lot.n_lot_id and stakeholder.n_stakeholder_id=lot.n_stakeholder_id  and bidact.c_bidded!='Y'"+
  39.                             "and bidact.c_branch_id='" + branchId + "' and bidact.c_org_id='"+ orgId +"' and bidact.c_state_id='" + stateId + "' "+
  40.                             "and bidact.c_region_id='" + regionId + "'  order by bidact.c_item_name,"+
  41.                             "bidact.d_bid_end_date, bidact.d_bid_end_time";
  42.  
  43. System.out.println("Output from dopost..........................");
  44. System.out.println("after query..................................");
  45.       st = con.createStatement();
  46.       st.executeQuery (sql);
  47.       rs = st.getResultSet();
  48.       System.out.println("result" +rs);
  49.  
  50.       System.out.print("before while");
  51.       while (rs.next()){
  52.         //Add records into data list
  53.           System.out.println("Hello....................................");
  54.         lot_ref= rs.getString(1);
  55.         System.out.print("refnum"+lot_ref);
  56.         lot_wt=rs.getString(2);
  57.        System.out.print("lotwt"+lot_wt);
  58.         item= rs.getString(3);
  59.        System.out.print("item"+item);
  60. //       dataList.add(rs.getString("lot.n_lot_ref_no"));
  61. //        dataList.add(rs.getString("bidact.c_item_name"));
  62. //        dataList.add(rs.getString("bidact.m_lot_wieght"));
  63.  
  64.  
  65.       }
  66. System.out.print("after while");
  67.  
  68.  
  69.  
  70.  
  71.       }catch(Exception e){
  72.       System.out.println("Exception is ;"+e);
  73.       }
  74.  
  75.  
  76.  
  77.   /*if(request.getParameter("strUserName")!=null && request.getParameter("strPassword")!=null)
  78.     {
  79.         String username = request.getParameter("strUserName").toString();
  80.        String passwd = request.getParameter("strPassword").toString();
  81.     }*/
  82.     // if (uName.equals(userName) && passwd.equals(password)){
  83.  
  84.   try
  85.   {
  86.     DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  87.         DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  88.         //creating a new instance of a DOM to build a DOM tree.
  89.         Document doc = docBuilder.newDocument();
  90.         new xmlServlet().createXmlTree(doc,lot_ref, lot_wt,item);
  91.  
  92.  
  93.     pw.println("<b>Xml File Created Successfully</b>");
  94.   }
  95.   catch(Exception e)
  96.   {
  97.     System.out.println(e);
  98.   }
  99.     try{
  100.     rs.close ();
  101.       st.close ();
  102.       con.close();
  103.     }catch(Exception e){
  104.       e.printStackTrace();
  105.     }
  106.     /* }
  107.      else
  108.      {
  109.      System.out.println("Invalid User");
  110.      }*/
  111.  
  112.   }
  113.  
  114.   public void createXmlTree(Document doc, String lot_ref, String lot_wt, String item) throws Exception {
  115.         //This method creates an element node
  116.         Element root = doc.createElement("User");
  117.         //adding a node after the last child node of the specified node.
  118.         doc.appendChild(root);
  119.  
  120.  
  121.  
  122.         Element child1 = doc.createElement("lot_ref");
  123.         root.appendChild(child1);
  124.  
  125.         Text text = doc.createTextNode(lot_ref);
  126.         child1.appendChild(text);
  127.  
  128.  
  129.  
  130.         Element element = doc.createElement("lot_wt");
  131.         root.appendChild(element);
  132.  
  133.         Text text1 = doc.createTextNode(lot_wt);
  134.         element.appendChild(text1);
  135.  
  136.         Element element1 = doc.createElement("item");
  137.         root.appendChild(element1);
  138.  
  139.         Text text2 = doc.createTextNode(item);
  140.         element.appendChild(text2);
  141.  
  142.  
  143.  
  144.  
  145.      //TransformerFactory instance is used to create Transformer objects.
  146.         TransformerFactory factory = TransformerFactory.newInstance();
  147.         Transformer transformer = factory.newTransformer();
  148.  
  149.         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  150.  
  151.  
  152.         // create string from xml tree
  153.         StringWriter sw = new StringWriter();
  154.         StreamResult result = new StreamResult(sw);
  155.         DOMSource source = new DOMSource(doc);
  156.         transformer.transform(source, result);
  157.         String xmlString = sw.toString();
  158.  
  159.         File file = new File("c:/temp/tender.xml");
  160.         BufferedWriter bw = new BufferedWriter
  161.                       (new OutputStreamWriter(new FileOutputStream(file)));
  162.         bw.write(xmlString);
  163.         bw.flush();
  164.         bw.close();
  165.  
  166.     }
  167.  
  168.  
  169. }
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
Mar 24 '10 #1
2 3215
Dheeraj Joshi
1,123 Expert 1GB
First of all, you have posted your question in the wrong place.

Have you tried to code? What problem you are getting?

Regards
Dheeraj Joshi

* When i was typing this question Dormilich moved this thread
Mar 24 '10 #2
jkmyoung
2,057 Expert 2GB
It seems you can create each row in the while(rs.next()) loop. Why not add your nodes there? Have a function kind of like createXmlTree(), say instead createXMLBranch(), and add the node created to your main document node.

Also, let's focus on making the xml file before you get caught up in display.
Mar 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to...
3
by: Jack | last post by:
Hi, I have a form when loaded, retrieves record from an access table. Among other fields there is a check box called FinalUpdate. This is tied to a...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into...
1
ddtpmyra
by: ddtpmyra | last post by:
how can I capture the query result in PHP? I have two queries below: # Fetch the file information $query ="update filestorage set approved...
2
by: jatin32 | last post by:
Hi, I have to export query result to EXCEL file in certain location. how do I do that? I have used DoCmd.OutputTo acOutputQuery,...
5
xxoulmate
by: xxoulmate | last post by:
i wanted to export the query result into csv file is there any way to do it., e.g. select field1,field2 from table into csvfile
1
by: titli | last post by:
Hi Friends, I have some code in an already built application. Now the output pivots in .xls files is not matching with input data. The...
25
by: NDayave | last post by:
How do, I have a form that outputs addresses in a format that can be printed on to 3x7 label paper for envelopes. What I want is a way to enter...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.