jt400 Trunc my where condition as like 子句

jt400 Trunc my where condition as like clause

我正在使用 jt400-9.3.jar 连接 DB2/AS400。

我的tableBAND有这个记录:

+-----|------------------+
| MAT |     NAME         |
+-----|------------------+
|100  | Paul McCartney   |
|101  | John Lennon      |
|102  | Ringo Starr      |
|103  | George Harrison  |

还有我的 table MUSICIAN 和:

+------|------------------+
|MAT   |  NAME            |
+------|------------------+
|1001  | Pete Best        |
|1002  | Stuart Sutcliffe |
|1003  | Jimmy Nicol      |
|1004  | Tommy Moore      |
|1005  | Norman Chapman   |

当我运行这个select

SELECT t.mt, t.name
FROM (      
    SELECT 
        trim(b.mat) AS mat, 
        trim(b.name) AS name
    FROM band b
    WHERE trim(b.mat) = '1001'
    UNION 
    SELECT 
        trim(m.mat) AS mat, 
        trim(m.name) AS name
    FROM MUSICIAN m
    WHERE trim(m.mat) = '1001'
) AS t
FETCH FIRST 1 ROWS ONLY

我检索:

+-----|----------------+
|MAT  |NAME            |
+-----|----------------+
|100  | Paul McCartney |

第一个查询作为 like 子句。 1001匹配的是100,但是句子是=没有like.

当我在 dbeaver 中执行查询时有效,但在 java(使用 PreparedStatement)中我出错了,驱动程序有一些配置吗?

John Eberhard 对我的问题回答得如此之快。

https://sourceforge.net/p/jt400/bugs/121/

正在注册您的答案。

The problem is that = is being used in the query. Because = is used in the query, then the database tells the driver that the type is CHAR(5). The driver then truncates to char 5.

There are two possible solutions.

  1. Add a cast to the parameter marker so that the larger character will fit and miscompare. i.e. SELECT * FROM MYTABLE WHERE MAT = CAST( ? AS VARCHAR(80))

  2. Use the "query replace truncated parameter" JDBC property. See https://static.javadoc.io/net.sf.jt400/jt400/9.7/com/ibm/as400/access/doc-files/JDBCProperties.html

Here is how that property is defined.

"query replace truncated parameter"

Specifies the value that should be used in place of a truncated parameter of an SQL query. By default, the parameter is silently truncated to the length for the parameter. Consider the following scenario.

Table T1 has a CHAR(3) column with the name of C1, and a row where C1='ABC'. An application prepares a statement using SELECT * FROM TABLE_X where C1=? If the parameter is set to 'ABCD', it wil be silently truncated to 'ABC' and a row will be returned by the query.

This property avoids this problem by allowing the application to set the string to something that doesn't exist in the application - i.e. @@@@@@@. A blank value means that the property will be ignored.