details make a difference! jdbc's List<?> qryList4Sql (String sql) error - identifier is too long

Background of the problem:

When writing sql, I was lazy because I thought it was simple and straightforward, and didn't pay attention to the details.

Operation steps and problems found:

1. The execution of sql statements uses the List<?> qryList4Sql(String sql) method provided by jdbc

2. This is my sql statement (simplified processing)

 String sql ="select  a,b,c from ABC WHERE ID  = " + paramForm.getSubId();

3. An error is reported during execution

org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select  a,b,c from ABC WHERE ID= XXXXXXXXXXX]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00972: 标识符过长

It's strange. I took out this sql and put it in the database to execute it directly. I found that there was no problem.

4. At first, I thought that the query fields were too long and too many, so I used aliases, but an error was still reported.

5. But I have used this query method before and no error was reported, so I compared and checked it. Because the two SQLs were written in different ways, the correct way to write them is like this

 String sql ="select  a,b,c from ABC WHERE 1 = 1 " 

if (ID != null && !ID.isEmpty()) {
            sql += " AND ID= '" + ID+ "'";
        }

I saw the reason. It turned out to be the original way of writing it. The specific data of the ID was not in quotation marks, so it could not be recognized when executed. It was executed as a whole, so an error was reported.


 

Guess you like

Origin blog.csdn.net/weixin_42450130/article/details/133011884