Java Spring Bean的Oracle DB连接问题

时间:2012-10-16 08:53:07

标签: java spring connection oracle11g

实际上我试图调用queryForInt()方法,但它显示NULL异常。当我使用以下代码访问具有相同凭据的dB时:

----------------这是工作-----------------------

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUsername("userName");
dataSource.setPassword("pwd");

------在Bean -----------------(这不行)                                          

<!-- DAOS -->
<bean id="ProductDAO" class="br.edeploy.voive.dao.ProductDAO">
    <property name="dataSource" ref="dataSource"/>
</bean>

---------关于ProductDAO类--------------(当我使用Spring方法时抛出此异常)

try
{
  int i = getSimpleJdbcTemplate().queryForInt("Select * from ERP_PRODUCT", "");
}
catch(DataAccessException dax)
{
  throw new Exception("Error: "+dax.getMessage());
}

感谢。

1 个答案:

答案 0 :(得分:0)

Spring jdbcTemplate.queryForInt()支持以下参数

 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.

您传递参数的参数,不确定为什么使用第二种方法而不是第一种方法(当您不需要传递参数时)

修改 使用queryForInt()的方法是:

String sql = "SELECT COUNT(*) FROM CUSTOMER";

int total = getJdbcTemplate().queryForInt(sql);

查看代码,看起来需要使用queryForObject()

String sql = "SELECT NAME FROM CUSTOMER WHERE CUST_ID = ?";

String name = (String)getJdbcTemplate().queryForObject(
            sql, new Object[] { custId }, String.class);