【问题标题】:java.sql.SQLException: Invalid column index using jdbcTemplatejava.sql.SQLException:使用 jdbcTemplate 的列索引无效
【发布时间】:2016-12-21 09:48:07
【问题描述】:

我是 JAVA 新手。我有一个配置文件

    <?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>

然后是我的代码

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);
    }
}

当我运行它时,它返回错误无效的列索引。谁能帮帮我?

谢谢

【问题讨论】:

  • 它缺少 where 子句。从 Test a where = ? 中选择 a.firstname

标签: java jdbctemplate


【解决方案1】:

您正在使用 queryForObject(java.lang.String, java.lang.Object[], java.lang.Class) ,它需要一个对象数组绑定到带有占位符的 PreparedStatement 类型查询。

您的查询可能缺少占位符,例如:

select a.firstname from TEST a where id = ?

您会收到有关无效列索引的错误,因为您的数组内容当前无法映射到任何占位符索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多