2014-03-29 45 views
0
<?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-> EXTERNAL LIBRARIES->添加jar文件時,纔會運行。如果我不在這裏添加,它不運行,它說ClassNotFoundException org.hsqldb.jdbc.JDBCDriver。騾ClassNotFoundException的:jdbcDriver

2)當我從騾子上運行,它說的ClassNotFoundException:org.hsqldb.jdbc.JDBCDriver

我已經添加在騾子的hsqldb.jar工作室 - >項目 - >屬性 - >構建路徑 - > libraries->添加外部庫。

錯誤信息:

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。

回答

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

0

這是因爲該類的名稱(在2.x中)是org.hsqldb.jdbc.JDBCDriver

+0

當我將驅動程序名稱更改爲以上驅動程序名稱時,它會顯示以下錯誤: SyntaxError :(''invalid syntax',''',3,25,'\ t \ t \ tClass.forName(「org.hsqldb .jdbcDriver「);')) – bekur

+0

不,它不。該編譯時錯誤仍然顯示舊名稱;你改變了別的東西。 – chrylis

+0

編輯器可能會出現問題,我在mule工作室創建了新項目,現在得到的錯誤是: java.lang.ClassNotFoundException:java.lang.ClassNotFoundException:org.hsqldb.jdbc.JDBCDriver – bekur

1

我曾與MySQL驅動程序類似的問題,這似乎是在騾3.4和3.5(3.3沒有失敗的)問題。 Mule在類路徑中找不到驅動程序,如果它包含在項目中。 我所做的是把驅動程序添加到運行時(騾服務器)的類路徑:

在Anypoint

[Anypoint_folder]\plugins\org.mule.tooling.server.3.5.0.ee_3.5.0.201407101856\mule\lib\opt

[org.mule.tooling.server.3.5.0.ee_3.5.0。 201407101856]取決於您安裝的版本。如果更新Anypoint,則可能需要再次複製del jdbc驅動程序,因爲運行時更新會創建一個新文件夾。

騾獨立運行:

[mule-enterprise-standalone-3.5.0]\lib\opt

如果你要部署到一個servlet容器,如Tomcat:

[apache-tomcat-6.0.39]\lib

0

你應該在騾子手動,然後添加JDBC驅動程序jar文件的JDBC連接器運行它。您也可以通過使用TestConnection選項確保測試連接是否成功。

相關問題