java.sql.SQLException:使用jdbcTemplate的无效列索引 [英] java.sql.SQLException: Invalid column index using jdbcTemplate

查看:57
本文介绍了java.sql.SQLException:使用jdbcTemplate的无效列索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

然后是我的代码

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 ?

谢谢

推荐答案

您正在使用

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.

这篇关于java.sql.SQLException:使用jdbcTemplate的无效列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆