JavaSpringbean的Oracle数据库连接问题
实际上,我试图调用queryForInt()方法,但它显示NULL异常。当我使用以下代码以相同凭据访问数据库时:JavaSpringbean的Oracle数据库连接问题,java,spring,connection,oracle11g,Java,Spring,Connection,Oracle11g,实际上,我试图调用queryForInt()方法,但它显示NULL异常。当我使用以下代码以相同凭据访问数据库时: <!-- DAOS --> <bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO"> <property name="dataSource" ref="dataSource"/> </bean> ----------------这是有效的-----------
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
----------------这是有效的-----------------------
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUsername("userName");
dataSource.setPassword("pwd");
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
------在Bean中----------------(这不起作用)
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
谢谢。Spring jdbcTemplate.queryForInt()支持以下参数
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
int queryForInt(String sql)
Execute a query that results in an int value, given static SQL.
int queryForInt(String sql, Object[] args)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.
int queryForInt(String sql, Object[] args, int[] argTypes)
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, resulting in an int value.
您为参数传递参数,但不确定为什么要使用第二个方法而不是第一个方法(当您不需要传递参数时)
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
编辑
使用queryForInt()的方法是:
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
查看您的代码,似乎需要使用queryForObject()
<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
你能添加stacktrace吗?有一点我不明白:您构建数据源时执行
drivermagerdatasource datasource=newdrivermagerdatasource()代码>但是您可以使用对数据源的引用将其注入到产品DAO
。显然,您在第一个代码段中构建的数据源与您在第二个代码段中注入的数据源不是同一个实例。但是,如果我们看到更多您的配置,我们可能会理解。嗨,实际上我之前试过,但没有成功。你能告诉我如何在我的Servlet类中调用这个名为“GetProduct()”的ProductDAO类的方法吗?