HSQLDB prepareStatement("SET SCHEMA ?") 错误

Posted

技术标签:

【中文标题】HSQLDB prepareStatement("SET SCHEMA ?") 错误【英文标题】:HSQLDB prepareStatement("SET SCHEMA ?") error 【发布时间】:2016-11-14 08:40:55 【问题描述】:

我正在编写一个需要与 HSQLDB 连接的 Jbatch 服务,但在某个时候它会抛出一个 NullPointerException (and some SQLExceptions/PersistenceExceptions) 。调试我发现在类 JDBCPersistenceManagerImpl.java 中它将 SCHEMA 设置为?,但在 HSQLDB 中似乎不支持它。

private void setSchemaOnConnection(Connection connection) throws SQLException 
        logger.finest("Entering " + CLASSNAME +".setSchemaOnConnection()");

        if (!"Oracle".equals(connection.getMetaData().getDatabaseProductName())) 
            PreparedStatement ps = null;
            ps = connection.prepareStatement("SET SCHEMA ?");  -->ps = NullPointerException 
            ps.setString(1, schema);
            ps.executeUpdate(); 
            ps.close();
        

        logger.finest("Exiting " + CLASSNAME +".setSchemaOnConnection()");
    

关于如何解决它的任何想法?我已经尝试过另一个数据库(H2 db),但问题是一样的。

这是完整的堆栈跟踪:

javax.batch.operations.JobStartException: com.ibm.jbatch.container.exception.PersistenceException: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException

    at com.ibm.jbatch.container.api.impl.JobOperatorImpl.start(JobOperatorImpl.java:92)
    at factory.jf.privacy.service.loggedareasfilter.batch.AzzaTest.testBatchChunkException(AzzaTest.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.ibm.jbatch.container.exception.PersistenceException: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
    at com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl.createJobInstance(JDBCPersistenceManagerImpl.java:1682)
    at com.ibm.jbatch.container.jobinstance.JobExecutionHelper.getNewJobInstance(JobExecutionHelper.java:89)
    at com.ibm.jbatch.container.jobinstance.JobExecutionHelper.startJob(JobExecutionHelper.java:120)
    at com.ibm.jbatch.container.impl.BatchKernelImpl.startJob(BatchKernelImpl.java:124)
    at com.ibm.jbatch.container.api.impl.JobOperatorImpl.startInternal(JobOperatorImpl.java:123)
    at com.ibm.jbatch.container.api.impl.JobOperatorImpl.start(JobOperatorImpl.java:88)
    ... 28 more
Caused by: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl.setSchemaOnConnection(JDBCPersistenceManagerImpl.java:396)
    at com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl.getConnection(JDBCPersistenceManagerImpl.java:334)
    at com.ibm.jbatch.container.services.impl.JDBCPersistenceManagerImpl.createJobInstance(JDBCPersistenceManagerImpl.java:1670)
    ... 33 more

【问题讨论】:

【参考方案1】:

编译这个准备好的语句会产生一个NullPointerExcepionhsqldb

Caused by: org.hsqldb.HsqlException: java.lang.NullPointerException
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.result.Result.newErrorResult(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 41 more
Caused by: java.lang.NullPointerException
    at org.hsqldb.ParserCommand.compileSet(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
    at org.hsqldb.Session.compileStatement(Unknown Source)
    at org.hsqldb.StatementManager.compile(Unknown Source)
    ... 42 more

但是,当您将其更改为不带占位符的语句时,它会起作用:

ps = connection.prepareStatement("SET SCHEMA "+schema);
ps.execute(); 

【讨论】:

他不能改类,在JDBCPersistenceManagerImpl.java中 javax.batch.operations.JobStartException: com.ibm.jbatch.container.exception.PersistenceException: java.sql.SQLException: java.lang.NullPointerException java.lang.NullPointerException 我用异常的完整堆栈跟踪编辑了问题 SVN base/trunk 中最新的 HSQLDB 代码支持这一点。您需要签出并构建 jar。

以上是关于HSQLDB prepareStatement("SET SCHEMA ?") 错误的主要内容,如果未能解决你的问题,请参考以下文章

hsqldb PreparedStatement 参数类型

在 prepareStatement 中使用 COALESCE 来“插入”cmd

Sybase JDBC 获取生成的密钥

JDBC PrepareStatement对象执行批量处理实例

jdbc 中prepareStatement对sql中的占位符赋值问题

prepareStatement与Statement的区别