2012-06-19 63 views
0

我想知道Derby是否支持SQL語句中的位運算符。 我可以在功能列表中看到真值測試爲YES。 以下同的鏈接:apache derby中的位運算符

http://db.apache.org/derby/docs/10.2/ref/rrefsql9241891.html 

我要執行這樣的查詢:SELECT 一個& b。從APP.TEST;

由於

試圖通過在德比定義一個函數實現上述功能:

CREATE FUNCTION BitAnd(a SMALLINT, b SMALLINT) RETURNS SMALLINT 
    PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA 
    EXTERNAL NAME 'derby.routines.BitAnd.bitAnd'; 

接着,我稱爲下面有德比類路徑, CALL SQLJ.INSTALL_JAR上述功能

('path\to\db\functions.jar', 'APP.functions', 0); 

CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY

('derby.database.classpath', 'APP.functions'); 

成功執行上述SP電話,隨後JAR目錄與下面的文件創建爲path\to\db\jar\APP\FUNCTIONS.jar.G1340195863482

但是當我嘗試在我的Java代碼調用這個函數:

deleteAgentStats = dbConnection.prepareStatement("select bitAnd(a,b) AS \"result\" from APP.TEST"); 
ResultSet andResult = deleteAgentStats.executeQuery(); 
if(andResult.next()){ 
     System.out.println("heyyyyyyyyyyyyyyyyyyyyyyyyy :: 
"+andResult.getInt("result")); 
} 

我得到以下例外:

Caused by: java.sql.SQLSyntaxErrorException: The class 'derby.routines.BitAnd' does not exist or is inaccessible. This can happen if the class is not public. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source) 
    at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source) 
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source) 
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281) 
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313) 
    at primary.db.AgentStatisticsDAO.deleteAgentStatistics(AgentStatisticsDAO.java:507) 
    ... 11 more 
Caused by: java.sql.SQLException: The class 'derby.routines.BitAnd' does not exist or is inaccessible. This can happen if the class is not public. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) 
    ... 28 more 
Caused by: java.sql.SQLException: Java exception: 'derby.routines.BitAnd: java.lang.ClassNotFoundException'. 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source) 
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source) 
    at org.apache.derby.impl.jdbc.Util.javaException(Unknown Source) 
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
    ... 25 more 
Caused by: java.lang.ClassNotFoundException: derby.routines.BitAnd 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1643) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1488) 
    at java.lang.ClassLoader.loadClassInternal(Unknown Source) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Unknown Source) 
    at org.apache.derby.impl.services.reflect.ReflectClassesJava2.loadClassNotInDatabaseJar(Unknown Source) 
    at org.apache.derby.impl.services.reflect.DatabaseClasses.loadApplicationClass(Unknown Source) 
    at org.apache.derby.iapi.services.loader.ClassInspector.getClass(Unknown Source) 
    at org.apache.derby.iapi.services.loader.ClassInspector.accessible(Unknown Source) 
    at org.apache.derby.impl.sql.compile.QueryTreeNode.verifyClassExist(Unknown Source) 
    at org.apache.derby.impl.sql.compile.StaticMethodCallNode.bindExpression(Unknown Source) 
    at org.apache.derby.impl.sql.compile.JavaToSQLValueNode.bindExpression(Unknown Source) 
    at org.apache.derby.impl.sql.compile.ResultColumn.bindExpression(Unknown Source) 
    at org.apache.derby.impl.sql.compile.ResultColumnList.bindExpressions(Unknown Source) 
    at org.apache.derby.impl.sql.compile.SelectNode.bindExpressions(Unknown Source) 
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bindExpressions(Unknown Source) 
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source) 
    at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source) 
    at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source) 
    at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source) 
    at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source) 
    ... 21 more 

請幫忙。

回答

2

這可能是使用Derby的Java可擴展性的好例子。考慮用Java編寫你的位操作例程,然後從你的Derby SQL中調用它們:http://wiki.apache.org/db-derby/DerbySQLroutines

+0

我創建了一個德比函數,但我無法調用該函數。在調用該函數時,我總是得到'ClassNotFoundException'。 – user748316

+1

那麼,您的代碼必須在Derby類路徑中可用,因爲它是調用您的代碼的Derby引擎。在嵌入式配置中,這意味着它必須位於應用程序類路徑中。在客戶端/服務器配置中,這意味着它必須位於網絡服務器的類路徑中。這裏有一個很好的博客,涵蓋以下主題:http://www.danvega.org/blog/2010/2/17/Creating-Apache-Derby-Custom-Functions-Part-2 –