Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Passing XML string to a stored procedure?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 733
    Answer it

    I am really really confused about how to convert string data that I get from a form and convert it to a XMLstring and pass the XML string to a stored procedure in Java. Please help me out.

    So, Let's say:-

    Post () method-
    String a = request.getParameter("FirstName");
    String b = request.getParameter("LastName");
    
    Now, stored procedure expects three parameters:-
    a. firstname
    b. lastname
    c. Details - xml datatype
    
    mystmt = conn.prepareCall("{call p_addeditcustomer(?,?,?)}");
    mystmt.setString("FirstName",a);
    mystmt.setString("LastName",b);
    mystmt.setString("Details",details);
    mystmt.executeQuery();

    Now here is the problem this is what the details parameter expects in xml format

    details-
    <Table1>
    <FirstName>a</FirstName>
    <LastName>b</LastName>
    </Table1>

    How do I convert the above fields into that xml string and pass it to the stored procedure please help me out? I am a beginner in JSP and servlets.

    For testing purpose I tried to pass the following String in a xml format but still it doesn't work

    String details = "<Table1> " + "<CustomerId> 10123</CustomerId>" + "</Table1>;

     

 1 Answer(s)

  • Try once this.

    String xml = "<Table1>"+ "<FirstName>" + a + "</FirstName>"+ "<LastName>" + b + "</LastName>"    + "</Table1>";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document details = db.parse(xml);
    mystmt.setObject("Details", details);
    

    Please let me know if that doesn't work.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: