java.sql.SQLException: Invalid column index using jdbcTemplate

扶醉桌前 提交于 2019-12-02 13:58:25

问题


I'm a newbie JAVA. I have a config file

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="org.springframework.docs.test" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@192.168.1.217:1521:ksoradev"/>
        <property name="username" value="pkrdm"/>
        <property name="password" value="mypass"/>
    </bean>
</beans>

and then my code

public class Main {
    public static void main(String args[]) throws Exception {
        ApplicationContext context = new ClassPathXmlApplicationContext("context.config.xml");
        DataSource dataSource = (DataSource) context.getBean("dataSource");
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String lastName = jdbcTemplate.queryForObject(
                "select a.firstname from TEST a",
                new Object[]{1212L}, String.class);
         System.out.println(lastName);
    }
}

When i run it, it return error Invalid column index. Can anyone help me ?

Thank


回答1:


You are using queryForObject(java.lang.String, java.lang.Object[], java.lang.Class) , which expects an array of objects to bind to a PreparedStatement type query with placeholders.

Your query is probably missing placeholders, e.g :

select a.firstname from TEST a where id = ?

You get errors about invalid column indexes, because the content of your array cannot currently be mapped to any placeholders indexes.



来源:https://stackoverflow.com/questions/41259701/java-sql-sqlexception-invalid-column-index-using-jdbctemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!