使用JDBC和Spring执行代码时出现异常

时间:2015-02-08 20:20:16

标签: java spring jdbc

这是xml文件中的内容

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.abhi" />
    <mvc:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="ds"

类= “org.springframework.jdbc.datasource.DriverManagerDataSource” &GT;                                                             

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="ds"></property>
    </bean>

    <bean id="edao" class="com.abhi.DataPersonDAO">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

</beans>`

这是我的DAO课程

public class DataPersonDAO {

    private JdbcTemplate jdbcTemplate;  

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
        this.jdbcTemplate = jdbcTemplate;  
    }  

    public int savePerson(Person p){  
        String query="insert into person values('"+p.getId()+"','"+p.getFirstName()+"','"+p.getLastName()+"','"+p.getBalance()+"','"+p.getInterest()+"')";  
        return jdbcTemplate.update(query);  
    }  

}

这是主要的

public class TestPerson {

    public static void main(String args[]) {

        System.out.println("Here U r in DAO 1");
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dispatcher-servlet.xml");
        System.out.println("Here U r in DAO 2 ");
        DataPersonDAO dao = (DataPersonDAO) ctx.getBean("edao");
        int status = dao.savePerson(new Person(102, "Amit", "Jain", 35000, 15000));
        System.out.println("Here U r in DAO");
        System.out.println(status);
    }
}

和Ofc Bean Class。

package com.abhi;

public class Person {

    public Person(int id, String firstName, String lastName, int balance,
            int interest) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.balance = balance;
        this.interest = interest;
    }
    private int id;
    private String firstName;
    private String lastName;
    private int balance;
    private int interest;

    public Person() {
        super();
    }
}

我添加了所有必需的jar和bean类,我试图使用spring和jdbc在数据库中添加数据但是这个程序显示错误

  

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:at oracle.jdbc.driver.JavaToJavaConverter.main(JavaToJavaConverter.java:2552)

我不知道我做错了什么

0 个答案:

没有答案