⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlbinding.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derbyTesting.functionTests.tests.lang.xmlBinding   Copyright 2005 The Apache Software Foundation or its licensors, as applicable.   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License. */package org.apache.derbyTesting.functionTests.tests.lang;import java.io.FileReader;import java.io.InputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;import java.sql.SQLException;import java.sql.Types;import org.apache.derby.tools.ij;import org.apache.derby.tools.JDBCDisplayUtil;/** * This class checks to make sure that the XML data type and * the corresponding XML operations all work as expected * from the JDBC side of things.  In particular, this test * verifies that 1) it is NOT possible to bind to/from an XML * datatype (because the JDBC specification doesn't indicate * how that should be done), and 2) the correct behavior * occurs when null values (both Java and SQL) are bound * into the bindable parameters for the XML operators. * This file also checks that insertion from XML files * via a character stream works, which is important since * XML files can be arbitrarily long and thus stream-based * processing is a must. */public class xmlBinding{    /**     * Create an instance of this class and do the test.     */    public static void main(String [] args)    {        new xmlBinding().go(args);    }    /**     * Create a JDBC connection using the arguments passed     * in from the harness, and then run the binding     * tests.     * @param args Arguments from the harness.     */    public void go(String [] args)    {        try {            // use the ij utility to read the property file and            // make the initial connection.            ij.getPropertyArg(args);            Connection conn = ij.startJBMS();            // Create our test table.            Statement st = conn.createStatement();            st.execute("create table xTable.t1 " +                "(i int generated always as identity, x xml)");            // Do the tests.            doBindTests(conn);            doXMLParseTests(conn);            doXMLSerializeTests(conn);            doXMLExistsTests(conn);            // Clean up.            st.close();            conn.close();            System.out.println("[ Done. ]\n");        } catch (Exception e) {            System.out.println("Unexpected error: ");            e.printStackTrace(System.out);        }    }    /**     * Performs a series of binding checks to make sure     * binding to an XML value never works.     * @param conn A connection to the test database.     */    private void doBindTests(Connection conn)    {        // Make sure that attempts to bind _to_ XML will fail.        System.out.println("\n[ Beginning XML binding tests. ]\n");        // Binding to an XML column.        PreparedStatement pSt = null;        try {            // If we're running in embedded mode or else with            // the Derby Client, then the next line will fail            // because there is NO deferred prepare.  If, however,            // we're running with JCC, the default is to defer            // the prepare until execution, so the next line will            // be fine, but the following four checks will fail.            // This difference in behavior okay--it requires two            // different masters, but ultimately it's a good way            // to check behavior in both cases.            pSt = conn.prepareStatement(                "insert into xTable.t1(x) values (?)");            System.out.print("XML column -- bind String to XML: ");            bindAndExecute(pSt, 1, Types.VARCHAR, "shouldn't work", "X0X14", false);            System.out.print("XML column -- bind Java null to XML: ");               bindAndExecute(pSt, 1, Types.VARCHAR, null, "X0X14", false);            System.out.print("XML column -- bind SQL NULL to XML: ");            bindAndExecute(pSt, 1, Types.VARCHAR, null, "X0X14", true);            System.out.print("XML column -- bind integer to XML: ");            bindAndExecute(pSt, 1, Types.INTEGER, new Integer(8), "X0X14", false);        } catch (SQLException se) {            // Must be running with embedded or Derby Network Client.            System.out.print("XML column -- insertion via parameter:  ");            checkException(se, "X0X14");        }        // Binding to an XML value in the XMLSERIALIZE operator.        // Should get compile-time error saying that         // parameters aren't allowed for XML data values.        System.out.print("Trying to bind to XML in XMLSERIALIZE: ");        try {            pSt = conn.prepareStatement(                "select XMLSERIALIZE(? AS CLOB) FROM XTABLE.T1");            bindAndExecute(pSt, 1, Types.VARCHAR, null, "X0X14", true);        } catch (SQLException se) {            checkException(se, "X0X14");        }        // Binding to an XML value in the XMLEXISTS operator.        // Should get compile-time error saying that         // parameters aren't allowed for XML data values.        System.out.print("Trying to bind to XML in XMLEXISTS: ");        try {            pSt = conn.prepareStatement(                "select i from xTable.t1 where " +                "XMLEXISTS('//*' PASSING BY VALUE ?)");            bindAndExecute(pSt, 1, Types.VARCHAR, null, "X0X14", true);        } catch (SQLException se) {            checkException(se, "X0X14");        }        // Make sure that attempts to bind _from_ XML will fail.        // We should fail at compile time, even before        // we get a chance to execute the query.        System.out.print("XML value in result set: ");        try {            pSt = conn.prepareStatement("select x from xTable.t1");            pSt.execute();        } catch (SQLException se) {            checkException(se, "X0X15");        }        System.out.println("\n[ End XML binding tests. ]\n");    }    /**     * Test insertion of documents larger than 32K (this     * will test stream processing of XML data), and     * test binding of null values in the XMLPARSE     * operator.     * @param conn A connection to the test database.     */    private void doXMLParseTests(Connection conn)    {        System.out.println("\n[ Beginning XMLPARSE tests. ]\n");        System.out.println("Test insertions from file: ");        try {             // Test parsing of > 32K XML documents.            insertFiles(conn, "xTable.t1", "xmlTestFiles/wide40k.xml", 1);            insertFiles(conn, "xTable.t1", "xmlTestFiles/deep40k.xml", 1);            // Test parsing of docs that use schemas.  Since server            // and client tests run in a subdirectory, we have to modify            // the XML documents that use DTDs so that they can find            // the DTD files.            insertDocWithDTD(conn, "xTable.t1", "xmlTestFiles/dtdDoc.xml",                "personal.dtd", 1);            insertFiles(conn, "xTable.t1", "xmlTestFiles/xsdDoc.xml", 1);            // XMLPARSE is not supposed to validate, so the following            // inserts should SUCCEED, even though the documents            // don't adhere to their schemas.            insertDocWithDTD(conn, "xTable.t1",                "xmlTestFiles/dtdDoc_invalid.xml", "personal.dtd", 1);            insertFiles(conn, "xTable.t1",                "xmlTestFiles/xsdDoc_invalid.xml", 1);            System.out.println("--> Insertions all PASS.");        } catch (SQLException se) {            System.out.println("FAIL: Unexpected exception: ");            while (se != null) {                se.printStackTrace(System.out);                se = se.getNextException();            }        } catch (Exception e) {            System.out.println("FAIL: Unexpected exception: ");            e.printStackTrace(System.out);        }        // Test binding nulls to the XMLPARSE operand.        try {            PreparedStatement pSt = conn.prepareStatement(                "insert into xTable.t1(x) values " +                "(XMLPARSE (DOCUMENT ? PRESERVE WHITESPACE))");            // This should work.  Note we check binding to            // a character stream method in "insertFiles".            System.out.print("Binding string in XMLPARSE: ");            bindAndExecute(pSt, 1, Types.CHAR, "<simple> doc </simple>",                null, false);            // Null should work, too.            System.out.print("Binding Java null string in XMLPARSE: ");            bindAndExecute(pSt, 1, Types.CHAR, null, null, false);            System.out.print("Binding SQL NULL string in XMLPARSE: ");            bindAndExecute(pSt, 1, Types.CLOB, null, null, true);        } catch (Exception e) {            System.out.println("Unexpected exception: ");            e.printStackTrace(System.out);        }        System.out.println("\n[ End XMLPARSE tests. ]\n");    }    /**     * Test serialization of the XML values inserted by     * the doXMLParseTests() method above.  For the documents     * that are larger than 32K, this tests that they can     * be correctly read from disk as a stream (instead of     * just as as string).     * @param conn A connection to the test database.     */    private void doXMLSerializeTests(Connection conn)    {        System.out.println("\n[ Beginning XMLSERIALIZE tests. ]\n");        try {            PreparedStatement pSt = conn.prepareStatement(                "select i, XMLSERIALIZE(X AS CLOB) FROM xTable.t1");            ResultSet rs = pSt.executeQuery();            String xResult = null;            int rowCount = 0;            while (rs.next()) {                xResult = rs.getString(2);                if (!rs.wasNull()) {                    System.out.println(rs.getInt(1) + ", " +                        "[ roughly " + (xResult.length() / 1000) + "k ]");                }                else                    System.out.println(rs.getInt(1) + ", NULL");                rowCount++;            }        } catch (Exception e) {            System.out.println("Unexpected exception: ");            e.printStackTrace(System.out);        }        // Test binding to the XMLSERIALIZE operand.  Since        // the operand is an XML value, and since we don't        // allow binding to an XML value (see "doBindTests()"        // above), there's nothing more to do here.        System.out.println("\n[ End XMLSERIALIZE tests. ]\n");    }    /**     * Run some simple XPath queries against the documents     * inserted in doXMLParseTests() above, and then test     * binding of null values in the XMLEXISTS operator.     * @param conn A connection to the test database.     */    private void doXMLExistsTests(Connection conn)    {        System.out.println("\n[ Begin XMLEXISTS tests. ]\n");        // Run some sample queries.        try {            existsQuery(conn, "xTable.t1", "//abb");            existsQuery(conn, "xTable.t1", "//d50");            existsQuery(conn, "xTable.t1", "//person/email");            existsQuery(conn, "xTable.t1", "/personnel");            existsQuery(conn, "xTable.t1", "//person/@id");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -