Skip to content

fix: When sql statements in mybatis query SQL Server data using clickhouse's jdbc function, #{} cannot set parameter values #1440

Closed

Description

@liuyudong-pacvue

Describe the bug

When I use mybatis, I need to find sqlserver data through the clickhouse jdbc function, but I found that sqlserver statements cannot use #{} to set parameters, but sql outside the jdbc function can use #{} to set parameters

Specifically, when I use the following SQL statement:

<select id="selectDownloadLogList" resultType="com.test.DownloadLogDTO">
    select * from jdbc ("commerce","select * from DownloadLog where id = #{query.id}") 
</select>

Error log

Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='query.id', mode=IN, javaType=class java.lang.Object, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.
Cause: java.sql.SQLException: Can't set parameter at index 1 due to no JDBC style '?' placeholder found in the query
Caused by: java.sql.SQLException: Can't set parameter at index 1 due to no JDBC style '?' placeholder found in the query
at com.clickhouse.jdbc.SqlExceptionUtils.clientError(SqlExceptionUtils.java:73)
at com.clickhouse.jdbc.internal.AbstractPreparedStatement.toArrayIndex(AbstractPreparedStatement.java:26)
at com.clickhouse.jdbc.internal.SqlBasedPreparedStatement.setLong(SqlBasedPreparedStatement.java:319)

Configuration

Environment

  • Client version: 0.4.6
  • Language version: jdk17
  • OS: windows11
  • DriverClass: com.clickhouse.jdbc.ClickHouseDriver

Activity

  1. liuyudong-pacvue commented on Sep 6, 2023

    @liuyudong-pacvue
    Author

    I found in com.clickhouse.jdbc.JdbcParameterizedQuery#parse method "ClickHouseUtils. IsQuote (ch)" will skip ['] and ["] in the middle of the content will not go to determine whether it will have[?] This makes it very difficult for me. Is there any good solution?

  2. zhicwu commented on Sep 6, 2023

    @zhicwu
    Contributor

    Hi @liuyudong-pacvue, it's expected that SQL parameter cannot be part of a string. Perhaps you can make the whole statement as a parameter?

    Although it could be fun to play with jdbc table function, but please do NOT use it on production. This is because that it has issue in both design and implementation. Besides stability issue, the most severe one is probably ClickHouse/ClickHouse#36385.

  3. added and removed on Sep 6, 2023
  4. SinceNovember commented on Sep 6, 2023

    @SinceNovember

    Hi, If using the jdbc function is not recommended, is there a better way to perform joins and other related operations with other types of databases? If so, please let me know

  5. zhicwu commented on Sep 7, 2023

    @zhicwu
    Contributor

    Hi @SinceNovember, if you're talking about federated query on ClickHouse, perhaps url table function is more reliable, although it requires you to implement a web service to bridge the query to corresponding database. If it's just about small amount of data for convenience as shown above, you may consider to issue separate query for external data, put it into an external table like below on ClickHouse for joining.

    static String exteralTables(String url) throws SQLException {
    String sql = "select a.name as n1, b.name as n2 from {tt 'table1'} a inner join {tt 'table2'} b on a.id=b.id";
    try (Connection conn = getConnection(url); PreparedStatement ps = conn.prepareStatement(sql)) {
    ps.setObject(1,
    ClickHouseExternalTable.builder().name("table1").columns("id Int32, name Nullable(String)")
    .format(ClickHouseFormat.CSV)
    .content(newInputStream("1,a\n2,b")).build());
    ps.setObject(2,
    ClickHouseExternalTable.builder().name("table2").columns("id Int32, name String")
    .format(ClickHouseFormat.JSONEachRow)
    .content(newInputStream("{\"id\":3,\"name\":\"c\"}\n{\"id\":1,\"name\":\"d\"}")).build());
    try (ResultSet rs = ps.executeQuery()) {
    if (!rs.next()) {
    throw new IllegalStateException("Should have at least one record");
    }
    // n1=a, n2=d
    return String.format("n1=%s, n2=%s", rs.getString(1), rs.getString(2));
    }
    }
    }

  6. liuyudong-pacvue commented on Sep 8, 2023

    @liuyudong-pacvue
    Author

    Thank you for your reply and I will refer to your comments!

  7. github-actions commented on Jan 8, 2025

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!

  8. added
    jdbc
    legacy
    queries
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jdbclegacyqueriesquestionstalewontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions