0

我试图复制以下问题中给出的相同示例。

import javax.sql.DataSource;    
import org.apache.camel.main.Main;
import org.apache.camel.builder.RouteBuilder;
import org.apache.commons.dbcp.BasicDataSource;

public class JDBCExample {

    private Main main;

    public static void main(String[] args) throws Exception {
        JDBCExample example = new JDBCExample();
        example.boot();
    }

    public void boot() throws Exception {
        // create a Main instance
        main = new Main();
        // enable hangup support so you can press ctrl + c to terminate the JVM
        main.enableHangupSupport();

        String url = "jdbc:oracle:thin:@MYSERVER:1521:myDB";
        DataSource dataSource = setupDataSource(url);

        // bind dataSource into the registery
        main.bind("myDataSource", dataSource);

        // add routes
        main.addRouteBuilder(new MyRouteBuilder());

        // run until you terminate the JVM
        System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }

    class MyRouteBuilder extends RouteBuilder {
        public void configure() {
            String dst = "C:/Local Disk E/TestData/Destination";
            from("direct:myTable")
               .setBody(constant("select * from myTable"))
               .to("jdbc:myDataSource")
                .to("file:" + dst);
        }
    }

    private DataSource setupDataSource(String connectURI) {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUsername("sa");
        ds.setPassword("devon1");
        ds.setUrl(connectURI);
        return ds;
    }
}

我在我的类路径中包含了 camel-jdbc-3.0.1.jar 和我的 db 特定 jar 文件。

当我尝试使用以下命令编译代码时

javac -cp .;D:\Code\bin JDBCExample.java

我收到以下错误。

JDBCExample.java:2: error: package org.apache.camel.main does not exist
import org.apache.camel.main.Main;
                            ^
JDBCExample.java:3: error: package org.apache.camel.builder does not exist
import org.apache.camel.builder.RouteBuilder;
                               ^
JDBCExample.java:4: error: package org.apache.commons.dbcp does not exist
import org.apache.commons.dbcp.BasicDataSource;

我哪里错了?我尝试将骆驼核心添加到类路径中,但没有帮助。

请让我知道您的想法,提前谢谢。

4

1 回答 1

1

通过将 camel-core 添加到类路径中您做得很好,但是 camel-core 和 camel-jdbc 还不够,您还应该添加以下依赖项:

JDBCExample.java:2:错误:包 org.apache.camel.main 不存在导入 org.apache.camel.main.Main;

添加骆驼主要依赖

JDBCExample.java:4:错误:包 org.apache.commons.dbcp 不存在导入 org.apache.commons.dbcp.BasicDataSource;

添加commons-dbcp依赖

JDBCExample.java:3:错误:包 org.apache.camel.builder 不存在导入 org.apache.camel.builder.RouteBuilder;

添加骆驼核心依赖

有了这些和camel-jdbc依赖项,你就可以开始了。

如果可以的话,我建议您使用 maven 来处理您的依赖项(以及更多)...如果您在这五分钟之前没有使用过它,快速入门会轻轻地向您介绍它。

这是一个示例 pom.xml 可以正确解析所有这些依赖项

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>camel-jdbc-demo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>camel-jdbc-demo</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-main -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-main</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jdbc -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jdbc</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
于 2020-02-26T03:18:21.947 回答