如何将XML插入Oracle数据库?

时间:2018-01-23 16:45:46

标签: java xml oracle

我尝试将XML插入到Oracle数据库中,就像保存的RandomQuestions.xml文件中显示的一样,我没有收到任何错误 但这就是它在数据库中出现的方式,我的QUESTIONS数据类型为VARCHAR2(4000 BYTE);

如果我使用ByteArrayOutputStream,在数据库中它看起来像这样: -

java.io.ByteArrayInputStream@38d21a74

不使用ByteArrayOutputStream: -

javax.xml.transform.stream.StreamResult@7138a214

RandomQuestions.xml如下所示: -

<GeneratedSet genBy="Tony Bond" genDate="23/01/2018">
    <Questions Level="1" NumQuestions="5" Type="Addition">
        <Question Answer="6.0" Text="5.0 + 1.0"/>
        <Question Answer="12.0" Text="9.0 + 3.0"/>
        <Question Answer="12.0" Text="5.0 + 7.0"/>
        <Question Answer="13.0" Text="8.0 + 5.0"/>
        <Question Answer="10.0" Text="3.0 + 7.0"/>
    </Questions>
</GeneratedSet>

我的代码: -

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    some code here ....

    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        // root elements
        Document doc = db.newDocument();

       some code here ...

        StringBuffer retXML = new StringBuffer("<GeneratedSet  genDate='").append(ft.format(todayDate)).append("' genBy='").append(userName).append("'>");

        if (add != null) {
            InputSource is = new InputSource();
            genMaths gm = new genMaths();
            retXML.append(gm.additionQ(leve, 2, numOfQuesAdd, 1));
        }
        retXML.append("</GeneratedSet>");
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(retXML.toString()));
        doc = db.parse(is);

        DOMSource source1 = new DOMSource(doc);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\Users\\XXXX\\Documents\\XML Files\\RandomQuestions.xml"));
        transformer.transform(source, result);

        /************** Adding questions to database **************/

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        TransformerFactory.newInstance().newTransformer().transform(source, result);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

        try {
            dbConn = new DbConnection();
            conn = DbConnection.connection();
            CallableStatement proc = null;
            proc = conn.prepareCall("{call pa_customer_admin.pr_create_question(?,?,?,?,?,?,?,?)}");

            proc.setInt(1, Integer.parseInt(assId));
            proc.setString(2, qusTypeCode);
            proc.setString(3, inputStream.toString());
            proc.setString(4, mark);
            proc.setString(5, timeLimit);
            proc.setString(6, cdocId);
            proc.registerOutParameter(7, OracleTypes.INTEGER);
            proc.registerOutParameter(8, OracleTypes.VARCHAR);
            proc.execute();

            String errorMessage = proc.getString(8);
            if (errorMessage == null) {
                System.out.println("No Error");
                quesId = proc.getString(7);
            } else {
                System.out.println(" Error : " + errorMessage);
            }
            proc.close();
            conn.close();
        } catch (SQLException e) {
            out.println("SQLException caught: " + e.getMessage());
            System.out.println("SQLException caught: " + e.getMessage() + "error Code : " + e.getErrorCode());
        } catch (NumberFormatException | ServletException | IOException e) {


        System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {
    } catch (SAXException | TransformerException ex) {
        Logger.getLogger(XmlServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

0 个答案:

没有答案