gpt4 book ai didi

java - Oracle: ORA-00911: 无效字符

转载 作者:行者123 更新时间:2023-12-02 03:05:07 25 4
gpt4 key购买 nike

在我的代码中,我有以下查询字符串:

   private static final String QUERY = format(
" SELECT t2.address " +
" FROM schema.table1 t1 ," +
" schema.table2 t2 ," +
" schema.table3 t3 ,"+
" schema.table4 t4 " +
" WHERE t2.uniqueIdentifier =:%s " +
" AND t1.parent_id = t2.parent_alias " +
" AND t3.company_id = t1.company_id " +
" AND t3.record_age = t2.recordAge " +
" AND t2.name = 'stubName' " +
" AND t4.pln_foi_id = t2.recordAge ",uniqueIdentifier);

在 native 查询中调用如下:

 public String getAddress(String uniqueIdentifier){

String result = null;

try {
Query query = persistence.entityManager().createNativeQuery(QUERY);
query.setParameter("uniqueIdentifier", uniqueIdentifier);
result = query.getSingleResult();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

当我测试此查询时,我得到以下结果:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Caused by: java.sql.SQLSyntaxErrorException: ORA-00911: invalid character

什么可能导致此错误?我在查询字符串或代码中看不到任何可能导致此问题的问题。

最佳答案

查询应该是

...
" WHERE t2.uniqueIdentifier = :uniqueIdentifier "
...

并删除对String.format()的调用;根据第一个 uniqueIdentifier 变量的值,您要么会受到 SQL 注入(inject),要么 setParameter() 将不起作用。

说明:当您有带参数的 native 查询时,您需要使用 :(冒号)前缀指定查询中的参数名称。要使用参数 foo,请将 :foo 放入查询中并调用 setParameter("foo", value); 来指定应使用哪个值用于代替参数。

关于java - Oracle: ORA-00911: 无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com