Mule ClassNotFoundException:jdbcDriver

时间:2014-03-29 23:15:42

标签: java xml mule jython

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" 
    xmlns:http="http://www.mulesoft.org/schema/mule/http" 
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 
    xmlns="http://www.mulesoft.org/schema/mule/core" 
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc"
    xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/jdbc http://www.mulesoft.org/schema/mule/jdbc/current/mule-jdbc.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

 <flow name="worldfiletestFlow1" doc:name="worldfiletestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
    <logger message="==========started=========" level="INFO" doc:name="Logger"/>
    <scripting:component doc:name="Python">
        <scripting:script engine="jython" >from java.lang import Class

            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            from java.sql import Statement, ResultSet, DriverManager
            connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost","sa","")
            statement = connection.createStatement()
            resultSet = statement.executeQuery("select * from test");
            while resultSet.next():
            print "%s (%s)" % (resultSet.getString("name"), resultSet.getString("personId"))
            resultSet.close();
            statement.close();
            connection.close();
        </scripting:script>
     </scripting:component>
     <logger message="=========END==========" level="INFO" doc:name="Logger"/>
 </flow>
</mule>

1)当我运行 Jython脚本时,来自db的数据将显示在控制台中。只有当hsqldb.jar被添加到pydev-PYTHONPATH-&gt; EXTERNAL LIBRARIES-&gt;时才会运行。添加jar文件。如果我不在这里添加,它就不会运行,它会说ClassNotFoundException org.hsqldb.jdbc.JDBCDriver。

2)当我从Mule运行时,它说ClassNotFoundException:org.hsqldb.jdbc.JDBCDriver

我在mule studio中添加了hsqldb.jar-&gt; project-&gt; properties-&gt; build path-&gt; libraries-&gt;添加外部库。

错误消息:

java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.hsqldb.jdbc.JDBCDriver

    at org.python.core.Py.JavaError(Unknown Source)
    at org.python.core.Py.JavaError(Unknown Source)
    at org.python.core.PyReflectedFunction.__call__(Unknown Source)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

我正在使用hsqldb2.3.2.jar。

4 个答案:

答案 0 :(得分:1)

我有一个与mysql驱动程序类似的问题,它似乎是mule 3.4和3.5中的一个问题(在3.3中没有失败)。 如果Mule包含在项目中,则Mule无法在类路径中找到该驱动程序。 我所做的是将驱动程序添加到运行时(mule服务器)类路径:

在Anypoint中

  

[Anypoint_folder] \插件\ org.mule.tooling.server.3.5.0.ee_3.5.0.201407101856 \骡\ lib中\选择

[org.mule.tooling.server.3.5.0.ee_3.5.0.201407101856] 取决于您安装的版本。如果更新Anypoint,则可能需要再次复制del jdbc驱动程序,因为运行时更新会创建一个新文件夹。

Mule独立运行时:

  

[骡-企​​业独立-3.5.0] \ lib中\选择

如果要部署到像Tomcat这样的servlet容器:

  

[Apache的Tomcat的6.0.39] \ lib中

答案 1 :(得分:0)

那是因为类的名称(在2.x中)是org.hsqldb.jdbc.JDBCDriver

答案 2 :(得分:0)

找到解决方案,

<mule ...
    <flow name="createFlow1" doc:name="createFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <scripting:component doc:name="Python">
            <scripting:script engine="jython" >from java.lang import Class
from java.sql import Statement, ResultSet, DriverManager

def importJar(jarFile):
    from java.net import URL, URLClassLoader
    from java.lang import ClassLoader
    from java.io import File
    m = URLClassLoader.getDeclaredMethod("addURL", [URL])
    m.accessible = 1
    m.invoke(ClassLoader.getSystemClassLoader(), [File(jarFile).toURL()])

importJar("C:/software/jar/hsqldb.jar")
importJar("C:/software/jar/sqljdbc4.jar")
Class.forName("org.hsqldb.jdbc.JDBCDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/testdb","sa","")
statement = connection.createStatement()
resultSet = statement.executeQuery("select * from Persons");
while resultSet.next():
    print "%s (%s)" % (resultSet.getString("firstname"), resultSet.getString("city"))
</scripting:script>
        </scripting:component>
    </flow>
</mule>

Groovy的解决方案:

    @Grapes([
     @Grab('org.slf4j:slf4j-simple:1.5.11'),
     @Grab('mysql:mysql-connector-java:5.1.12'),
     @GrabConfig(systemClassLoader = true)
    ])
    import groovy.sql.*
    ...
// Note, change jar accordingly.

默认情况下,依赖项在与Jython / Groovy脚本或应用程序相同的类加载器中可用。但有时这还不够。例如,如果我们有一个依赖于SQL数据库驱动程序的脚本,并且在我们的代码中使用java.sql.DriverManager.getConnection(),我们希望我们的驱动程序类位于系统类加载器上。这是因为DriverManager类只能“查看”系统类加载器中的类而不能查看其他类加载器。我们得到的一个可能的例外是java.sql.SQLException:找不到合适的驱动程序。

您可以查看以下讨论以获取更多详细信息:

system class loader

答案 3 :(得分:0)

你应该手动将Jdbc驱动程序jar文件添加到mule中的jdbc连接器然后运行它。同时你可以确保在那里使用TestConnection选项测试连接是否成功。