Querying data with JdbcTemplate error: Invalid column name (resolved)

Source: Internet
Author: User

Made a mistake again.

Fight for no next time.

Even if you do it again, you need to know where to find the answer.

So, take a note and take a look at the alert.

Error

When querying data using JdbcTemplate, an exception occurs:

PreparedStatementCallback; Bad SQL grammar [--sql--]; Nested exception is java.sql.SQLException: Invalid column name

The code is roughly as follows:

1list<someobj> list =getjdbctemplate (). query (SQL,2                 NewRowMapper () {3                      PublicObject Maprow (ResultSet arg0,intarg1)4                             throwsSQLException {5Someobj row =Newsomeobj ();6 7Row.setid (arg0.getstring ("ID"));8                         9                         returnRow;Ten                     } One});

Find the wrong

Since the column name is invalid , it must be a SQL issue.

As a result, the SQL posted to the PL/SQL execution, strange, no error.

Anti-second interview several times, are the program error, but directly execute SQL no problem.

Recall the previous problem because of the number of parameters, encountered such an exception: Java.sql.SQLException: Invalid column index.

So I checked the parameters again, no problem.

Can only search the Internet to find the answer.

Searched several articles, did not find and this similar question.

Looking at it, it suddenly occurred to me that the columns queried in SQL are consistent with the columns in the Java code to get them?

It turns out that.

The reason for the exception was found: There are no queries in SQL for the columns used in Java code.

That is, Java uses code like this: Row.setid (arg0.getstring ("Col_a")) , and the SELECT statement in SQL does not col_a this column.

PostScript: This is the adjustment of the previous code, the removal of some unnecessary columns, so that the SQL and the subsequent need to value the column inconsistent.

This problem should not arise in the new development.

Because such applications generally determine which columns are required and then assemble the SQL.

Querying data with JdbcTemplate error: Invalid column name (resolved)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.