无法将值插入从A​​pache Camel中的XML文件获取的Oracle数据库中

时间:2014-09-05 10:46:11

标签: oracle apache-camel stringbuilder

我正在进行概念验证,以便将XML值插入Oracle数据库。我在Java中使用StringBuilder来插入值。我的Camel背景是:

    <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="
                     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
                     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

              <bean id="dataSource" class="org.springframework.jdbc.datasource">
                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
                <property name="username" value="system"/>
                <property name="password" value="1234"/>
              </bean>
             <bean id="orderToSQL" class="camel.project.camel.system.OrderToSQLBean" />
             <camelContext xmlns="http://camel.apache.org/schema/spring">
                <route>
               <from uri="file:src/data?noop=true"/>
                    <choice>
                        <when>
                            <xpath>/person/city = 'London'</xpath>
                            <log message="UK message"/>
                            <to uri="file:target/messages/uk"/>
                        </when>
                        <when>
                            <xpath>/person/city = 'Charleston'</xpath>
                            <log message="US message"/>
                            <to uri="bean:orderToSQL?method=toSql"/>
                        </when>
                        <otherwise>
                            <log message="Other message"/>
                            <to uri="file:target/messages/others"/>
                        </otherwise>
                    </choice>
                </route>
            </camelContext>
             </beans>

因此,无论何时出现与城市查尔斯顿的消息,我都必须将该XML中的数据写入Oracle数据库。我正在使用Java类,如下所示。它不会抛出任何类型的错误,但它无法将值插入数据库。

     package camel.project.camel.system;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.ResultSet;
        import java.sql.ResultSetMetaData;
        import java.sql.SQLException;
        import java.sql.Statement;
        import javax.sql.DataSource;
        import org.apache.camel.language.XPath;

        public class OrderToSQLBean {

            /**
             * @param args
             */
            public static void main(String[] args) {
                 try {
                     Class.forName("oracle.jdbc.driver.OracleDriver" );
                     } catch (Exception e) {
                     System.err.println("ERROR: failed to load JDBC driver.");
                     e.printStackTrace();
                     }
                     try {
                        Connection c = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","tcs#1234");
                        Statement st = null;
                        ResultSet rs = null;
                        st = c.createStatement(); 
                        rs = st.executeQuery("SELECT *from people;"); 
                        dump(rs);
                        st.close();    
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

            }
            public static void dump(ResultSet rs) throws SQLException {
                ResultSetMetaData meta   = rs.getMetaData();
                int colmax = meta.getColumnCount();
                int i;
                Object o = null;
                for (; rs.next(); ) {
                    for (i = 0; i < colmax; ++i) {
                        o = rs.getObject(i + 1);
                        System.out.print(o.toString() + " ");
                    }
                    System.out.println(" ");
                }
            }  
            public String toSql(@XPath("person/firstName") String firstName,
                @XPath("person/lastName") String lastName,
                @XPath("person/city") String city)
            {   

               StringBuilder sb = new StringBuilder();
               sb.append("insert into people");
               sb.append("(country, lastname, firstname, city) values (");
               sb.append("'US', ");
               sb.append("'").append(lastName).append("', ");
               sb.append("'").append(firstName).append("', ");
               sb.append("'").append(city).append("'); ");
               System.out.println("String = " +sb);
               return sb.toString();
            }

        }

这是我在控制台中输出的输出:

[INFO] Using org.apache.camel.spring.Main to initiate a CamelContext
[pache.camel.spring.Main.main()] MainSupport                    INFO  Apache Camel 2.10.0.redhat-60024 starting
[pache.camel.spring.Main.main()] ClassPathXmlApplicationContext INFO  Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2cbe08: startup date [Fri Sep 05 15:50:42 IST 2014]; root of context hierarchy
[pache.camel.spring.Main.main()] XmlBeanDefinitionReader        INFO  Loading XML bean definitions from file [C:\Users\761285\Desktop\New Workspace\camel-system\target\classes\META-INF\spring\configuration.xml]
[pache.camel.spring.Main.main()] XmlBeanDefinitionReader        INFO  Loading XML bean definitions from file [C:\Users\761285\Desktop\New Workspace\camel-system\target\classes\META-INF\spring\xmltodb-context.xml]
[pache.camel.spring.Main.main()] DefaultListableBeanFactory     INFO  Overriding bean definition for bean 'dataSource': replacing [Generic bean: class [org.apache.commons.dbcp.BasicDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=close; defined in file [C:\Users\761285\Desktop\New Workspace\camel-system\target\classes\META-INF\spring\configuration.xml]] with [Generic bean: class [org.springframework.jdbc.datasource.SingleConnectionDataSource]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\Users\761285\Desktop\New Workspace\camel-system\target\classes\META-INF\spring\xmltodb-context.xml]]
[pache.camel.spring.Main.main()] DefaultListableBeanFactory     INFO  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10b2840: defining beans [dataSource,orderToSQL,template,consumerTemplate,camel-1:beanPostProcessor,camel-1]; root of factory hierarchy
[pache.camel.spring.Main.main()] SingleConnectionDataSource     INFO  Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.10.0.redhat-60024 (CamelContext: camel-1) is starting
[pache.camel.spring.Main.main()] ManagementStrategyFactory      INFO  JMX enabled.
[pache.camel.spring.Main.main()] DefaultTypeConverter           INFO  Loaded 175 type converters
[pache.camel.spring.Main.main()] FileEndpoint                   INFO  Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
[pache.camel.spring.Main.main()] FileEndpoint                   INFO  Using default memory based idempotent repository with cache max size: 1000
[pache.camel.spring.Main.main()] XPathBuilder                   INFO  Created default XPathFactory com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl@1b792e0
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Route: route1 started and consuming from: Endpoint[file://src/data?noop=true]
[pache.camel.spring.Main.main()] ultManagementLifecycleStrategy INFO  StatisticsLevel at All so enabling load performance statistics
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Total 1 routes, of which 1 is started.
[pache.camel.spring.Main.main()] SpringCamelContext             INFO  Apache Camel 2.10.0.redhat-60024 (CamelContext: camel-1) started in 0.319 seconds
[1) thread #0 - file://src/data] route1                         INFO  UK message
[1) thread #0 - file://src/data] route1                         INFO  Other message
[1) thread #0 - file://src/data] route1                         INFO  US message

我打印字符串我通过stringbuilder获取生成的查询是正确的oracle数据库查询语法

String = insert into people(country, lastname, firstname, city) values ('US', 'Peeples', 'Kenneth', 'Charleston'); 

请帮我解决这个问题。我没有使用任何蓝图XML。

1 个答案:

答案 0 :(得分:0)

你没有看到任何错误,这很奇怪。你的select和insert都有一个颤音的分号;这是一个语句分隔符,无效。你的插入应该是:

String = insert into people(country, lastname, firstname, city)
values ('US', 'Peeples', 'Kenneth', 'Charleston')

...没有分号。从您的选择中删除一个,这也需要在from

之前添加一个空格
st.executeQuery("SELECT * from people");

对于这两个语句,你应该得到ORA-00911,所以也许它根本就没有尝试执行它们。你正在返回sb,但我不熟悉Camel,所以我不知道什么时候或者是否实际执行了你返回的内容,或者你是否应该在{{1}内执行方法。

您还应该使用预准备语句和绑定变量,而不是通过StringBuilder将固定值放入字符串中,但这稍微超出了问题的范围。您应该阅读SQL注入。