0

我想知道 Derby 是否支持 SQL 语句中的位运算符。我可以在功能列表中看到真值测试为“是”。以下是相同的链接:

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

我想执行这样的查询:SELECT a & b FROM APP.TEST;

谢谢

尝试通过在 derby 中定义一个函数来实现上述功能:

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

接下来,我在 derby 类路径 CALL SQLJ.install_jar 上调用了以下函数以具有上述功能

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

调用 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

请帮忙。

4

1 回答 1

2

这可能是使用 Derby 的 Java 可扩展性的好例子。考虑用 Java 编写位操作例程,然后从 Derby SQL 调用它们:http ://wiki.apache.org/db-derby/DerbySQLroutines

于 2012-06-19T14:26:49.663 回答