[JDBI] [Java] JDBI .bind函数在此查询中不起作用

时间:2018-04-11 16:07:25

标签: java sql oracle jdbi

非常抱歉,如果这似乎没有充分研究或尝试不当。我是一个初学者,我已经在这几个小时了,现在尝试不同的变体:value,(:value),?,(?),bindBean,bind等无济于事。我在我的智慧结束。

我有一个包含4列的表,我希望将每个查询的内容(只返回一行)作为对象返回。

查询:

SELECT
    *
FROM
    schema.relation
WHERE
    entity = :value

这是查询。这是函数(为了清楚起见,我使用了一个8位数字来测试自己,比如12345678):

    public DatabaseEntry GetInfo(long value) {
        jdbi.registerRowMapper(DatabaseEntry.class,
                (rs, ctx) -> new DatabaseEntry(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4)));

        return Try.of(
                () -> jdbi.withHandle((HandleCallback<DatabaseEntry, SQLException>) handle ->
                        handle.select(Queries.DatabaseDetails())
                                .bind("value", value)
                                .map(new DatabaseEntryMapper())
                                .findOnly()
                )
        ).getOrElseThrow((Function<Throwable, RuntimeException>) RuntimeException::new);
    }

映射器类:

public class DatabaseEntryMapper implements RowMapper<DatabaseEntry> {

    @Override
    public DatabaseEntry map(ResultSet rs, StatementContext ctx) throws SQLException {

        final String valueone = rs.getString(1);
        final String valuetwo = rs.getString(2);
        final String valuethree = rs.getString(3);
        final String valuefour = rs.getString(4);

        return new DatabaseEntry(valueone, valuetwo, valuethree, valuefour);
    }
}

我遇到的问题是它似乎没有将参数值绑定到查询中的:value。这意味着db(oracle)将其转换为&#34;?&#34;。我无法通过阅读可用的文档找到一种方法来约束参数值(我不完全理解它,并且找不到展示这个的例子)。任何解释我出错的地方都将不胜感激。

这是错误消息:由以下原因引起:java.lang.IllegalStateException:仅在&#39;中找不到元素&#39;

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>SomeArtifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency> <!-- logger -->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency> <!-- lib for SQL -->
            <groupId>org.jdbi</groupId>
            <artifactId>jdbi3-core</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency> <!-- lib for connection pool / data source for SQL -->
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.7.8</version>
        </dependency>
        <dependency><!-- lib for functional java programming -->
            <groupId>io.vavr</groupId>
            <artifactId>vavr</artifactId>
            <version>0.9.2</version>
        </dependency>
        <dependency><!-- lib for unit testing -->
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency><!-- lib for microweb-development -->
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>LATEST</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>LATEST</version>
        </dependency>
        <!--<dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-template-velocity</artifactId>
            <version>LATEST</version>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

0 个答案:

没有答案