简体   繁体   中英

Insert query using jdbctemplate gives exception UncategorizedSQLException

I'm using jdbctemplate for insert query. This query works fine in SQL server studio. But when I use using jdbctemplate, this gives the exception of UncategorizedSQLException

Total exception

 org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO Test (ID, NAME) VALUES (?, ?)]; SQL state [null]; error code [0]; The conversion from UNKNOWN to UNKNOWN is unsupported.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.

My code is

sqlServerJdbcTemplate.update(
        "INSERT INTO Test (ID, NAME) VALUES (?, ?)",
        new Object[]{101, "Dave"},
        new Object[]{Types.INTEGER, Types.VARCHAR}
);

There are only two columns ID and NAME of Integer and varchar type.

What is the issue in this flow? Any ideas would be appreciated.

PS : SQL server is the DB

To avoid this problem you can remove new Object[]{Types.INTEGER, Types.VARCHAR} and make the DBMS detect the Type of each input :

sqlServerJdbcTemplate.update(
        "INSERT INTO Test (ID, NAME) VALUES (?, ?)",
        new Object[]{101, "Dave"}
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM