我是新来的斯普林斯。我正在尝试运行 JDBCTempalte 示例。我得到了 ClassCastException。这对我来说没有意义。
我的上下文文件
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- I have removed my config, I have tried JDBC code it is working with this datasource-->
</bean>
<bean id="jdbcDAO" class="com.sarma.spring.jdbcEx.JDBCDAOImpl">
<property name="dataSource" ref="dataSource"/><!-- This is my jdbc example it is working-->
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
我的主要课程
try{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
org.springframework.jdbc.core.JdbcTemplate jdbc = (org.springframework.jdbc.core.JdbcTemplate)context.getBean("jdbcTemplate");
String query = "SELECT ACNT_NBR FROM eit.ACNT WHERE ACNT_ID=13057";
int o= jdbc.queryForInt(query);
}catch(Exception e){
e.printStackTrace();
}
输出
2013-10-08 16:43:34 INFO ClassPathXmlApplicationContext:513 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c672d0: startup date [Tue Oct 08 16:43:34 EDT 2013]; root of context hierarchy
2013-10-08 16:43:34 INFO XmlBeanDefinitionReader:316 - Loading XML bean definitions from class path resource [applicationContext.xml]
2013-10-08 16:43:34 INFO DriverManagerDataSource:133 - Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
java.lang.ClassCastException: $Proxy12 cannot be cast to org.springframework.jdbc.core.JdbcTemplate
at com.sarma.spring.jdbcEx.main.JdbcExMain.main(JdbcExMain.java:45)
我的 45 行是
org.springframework.jdbc.core.JdbcTemplate jdbc = (org.springframework.jdbc.core.JdbcTemplate)context.getBean("jdbcTemplate");
我有错吗??谢谢你的帮助
谢谢,
CVSR 萨尔玛