jooq 返回具有错误值的查询

发布于 2025-01-07 18:19:13 字数 859 浏览 2 评论 0原文

我想使用以下基于 jooq 的代码插入数据库表数据来生成查询:

Factory jf = getJooqFactory();
int surveyId = jf.nextval(SURVEY_ID_SEQ).intValue();
jf.insertInto(SURVEY)
        .set(SURVEY.ID, surveyId)
        .set(SURVEY.NAME, survey.getName())
        .set(SURVEY.IDML, Factory.val(idml, SQLDataType.CLOB))
        .execute();

问题是,当我使用此代码打印查询时,

System.out.println(jf.insertInto(SURVEY)
            .set(SURVEY.ID, null)
            .set(SURVEY.NAME, null)
            .set(SURVEY.IDML, null)
            .getSQL());

我得到的是带有问号而不是值的查询。

insert into "collect"."survey" ("id", "name", "idml") values (?, ?, ?)

当我单独打印变量的值时,它们都是正确的。即使我在 .set() getSQL() 中手动插入字符串值也会返回问号。

I want to insert into database table data using following jooq based code to generate query:

Factory jf = getJooqFactory();
int surveyId = jf.nextval(SURVEY_ID_SEQ).intValue();
jf.insertInto(SURVEY)
        .set(SURVEY.ID, surveyId)
        .set(SURVEY.NAME, survey.getName())
        .set(SURVEY.IDML, Factory.val(idml, SQLDataType.CLOB))
        .execute();

The problem is that when I print the query using this code

System.out.println(jf.insertInto(SURVEY)
            .set(SURVEY.ID, null)
            .set(SURVEY.NAME, null)
            .set(SURVEY.IDML, null)
            .getSQL());

what I get is query with question marks instead of values.

insert into "collect"."survey" ("id", "name", "idml") values (?, ?, ?)

When I print values of variables separately they are all correct. Even if I insert String values manually in .set() getSQL() return question marks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2025-01-14 18:19:13

org.jooq.Query.getSQL( ) 呈现 SQL 的方式与呈现给 JDBC 的方式完全相同 PreparedStatement。 Java 中的准备语句期望绑定变量呈现为问号:?。然后使用方法设置实际的绑定值,如本例所示。

PreparedStatement stmt = connection.prepareStatement(
  "insert into collect.survey (id, name, idml) values (?, ?, ?)");
stmt.setInt(1, surveyId);
stmt.setString(2, survey.getName());
stmt.setString(3, idml);
stmt.executeUpdate();

如果您想查看生成的带有内联绑定值的 SQL,您可以使用工厂的 Factory.renderInlined()方法:

String sql = jf.renderInlined(
  jf.insertInto(SURVEY)
    .set(SURVEY.ID, surveyId)
    .set(SURVEY.NAME, survey.getName())
    .set(SURVEY.IDML, val(idml, SQLDataType.CLOB))
);

Query 对象上还有一个 getSQL(boolean) 方法,用于使用内联绑定变量呈现 SQL:

jf.insertInto(SURVEY)
  .set(SURVEY.ID, surveyId)
  .set(SURVEY.NAME, survey.getName())
  .set(SURVEY.IDML, val(idml, SQLDataType.CLOB))
  .getSQL(true);

了解有关 JDBC 的 PreparedStatement 的更多信息这里:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

org.jooq.Query.getSQL() renders SQL exactly as it is rendered to the JDBC PreparedStatement. Prepared statements in Java expect bind variables to be rendered as question marks: ?. The actual bind values are then set using methods, such as in this example

PreparedStatement stmt = connection.prepareStatement(
  "insert into collect.survey (id, name, idml) values (?, ?, ?)");
stmt.setInt(1, surveyId);
stmt.setString(2, survey.getName());
stmt.setString(3, idml);
stmt.executeUpdate();

If you want to see the generated SQL with inlined bind values, you can use the factory's Factory.renderInlined() method:

String sql = jf.renderInlined(
  jf.insertInto(SURVEY)
    .set(SURVEY.ID, surveyId)
    .set(SURVEY.NAME, survey.getName())
    .set(SURVEY.IDML, val(idml, SQLDataType.CLOB))
);

There is also a getSQL(boolean) method on the Query object, to render SQL with inlined bind variables:

jf.insertInto(SURVEY)
  .set(SURVEY.ID, surveyId)
  .set(SURVEY.NAME, survey.getName())
  .set(SURVEY.IDML, val(idml, SQLDataType.CLOB))
  .getSQL(true);

Learn more about JDBC's PreparedStatement here:

http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文