Java Oracle:ORA-00911:无效字符
在我的代码中,我有以下查询字符串:Java Oracle:ORA-00911:无效字符,java,string,oracle,hibernate,nativequery,Java,String,Oracle,Hibernate,Nativequery,在我的代码中,我有以下查询字符串: private static final String QUERY = format( " SELECT t2.address " + " FROM schema.table1 t1 ," + " schema.table2 t2 ," + " schema.table3 t3
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);
在本机查询中调用,如下所示:
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注入或setParameter()
将不起作用
说明:当您有一个带参数的本机查询时,您需要在查询中使用:
(冒号)前缀指定参数的名称。要使用参数foo
,请在查询中放入:foo
,然后调用setParameter(“foo”,value)
指定应使用哪个值代替参数。查询应为
...
" WHERE t2.uniqueIdentifier = :uniqueIdentifier "
...
并删除对String.format()
的调用;根据第一个uniqueIdentifier
变量的值,您将接受SQL注入或setParameter()
将不起作用
说明:当您有一个带参数的本机查询时,您需要在查询中使用
:
(冒号)前缀指定参数的名称。要使用参数foo
,请在查询中放入:foo
,然后调用setParameter(“foo”,value)
指定应使用哪个值来代替参数。t2.uniqueIdentifier=:%s
这是不正确的。请对此进行扩展或给出答案。变量uniqueIdentifier中应该包含哪些值?可能是导致问题的原因。它必须是类似于t2.uniqueIdentifier=?
格式的结果返回一个以SELECT t2开头的字符串。在我看来,
中的地址是,
不应该存在。t2.uniqueIdentifier=:%s
这不正确。请对此进行详细说明或给出答案。变量uniqueIdentifier中应该包含哪些值?这可能是导致问题的原因。它必须是类似于t2.uniqueIdentifier=?
格式的结果返回一个以SELECT t2开头的字符串。在我看来,,
不应该在那里。我已经尝试过了,但我得到了相同的错误。我认为查询语法肯定有其他问题。您可以记录查询和参数,并尝试在SQL工具中手动运行它吗?它们通常可以突出显示语法错误。如果失败,请尝试调试代码并查看SQLException内部。它将包含查询中有问题字符的偏移量。我已经尝试过了,我得到了相同的错误。我认为查询语法肯定有其他问题。您可以记录查询和参数,并尝试在SQL工具中手动运行它吗?它们通常可以突出显示语法错误。如果失败,请尝试调试代码并查看SQLException内部。它将包含查询中有问题字符的偏移量。