【发布时间】:2020-05-16 02:06:58
【问题描述】:
我正在尝试使用准备好的语句在 Java 中执行 LIKE 查询,但出现以下错误
ORA-00904: "%12P1A%": invalid identifier
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DataSourceFactory.getConnection();
statement = connection.prepareStatement("select * from users where userID like ?");
statement.setString(1, "%12P1A%");
resultSet = statement.executeQuery();
//....
}catch (SQLException e) {
throw new DAOException(e.getMessage());
} finally {
DaoUtil.closeAll(connection, statement, resultSet);
}
我可以知道为什么这是不正确的吗?
有关更多信息,我实际上是通过其他一些函数获得“%12P1A%”,因此代码类似于
statement = connection.prepareStatement("select * from users where userID like ?");
statement.setString(1, getValue());
查询解析为类似
select * from users where userID like '%12P1A%'
但它正在抛出 MISSING IN 或 OUT 参数。不知道为什么它没有选择价值。有什么建议?
【问题讨论】:
-
没错;至少您向我们展示的两行是正确的。请附上完整的代码。
-
列用户ID数据类型? (真的是人物类型吗?!?)
-
@TimBiegeleisen 这正是我想要运行的,rest 只是在 try catch 中执行查询。 @jarlh 是的,它是一个 varchar
-
statement必须是PreparedStatement并且查询必须是statement.executeQuery()(不重复 SQL 字符串)。还有statement不应该是一个字段,而应该是一个防止并发使用的局部变量。 -
提示:我不是 Java 专家,但是当字符串文字用双引号括起来时,我能够重现该问题:
select * from dual where 1 like "%12P1A%"-- ORA-00904: "%12P1A%":无效的标识符 -- 它必须作为'%12P1A%'传递 -- 如果有帮助
标签: java sql oracle prepared-statement oracle-sqldeveloper