Sunday, April 29, 2012

SPRING Returning Other Datatypes from JdbcTemplate

We returned an integer datatype result from our query in the previous tutorial. In this tutorial, we'll learn how to return other datatypes. We'll implement a method that returns a String query output.



 int org.springframework.jdbc.core.JdbcTemplate.queryForInt(String sql) throws DataAccessException
queryForInt
public int queryForInt(String sql)
                throws DataAccessException
Description copied from interface: JdbcOperations 
Execute a query that results in an int value, given static SQL. 
Uses a JDBC Statement, not a PreparedStatement. If you want to execute a static query with a PreparedStatement, use the overloaded queryForInt method with null as argument array. 


This method is useful for running static SQL with a known outcome. The query is expected to be a single row/single column query that results in an int value. 




Specified by:
queryForInt in interface JdbcOperations
Parameters:
sql - SQL query to execute 
Returns:
the int value, or 0 in case of SQL NULL 
Throws: 
IncorrectResultSizeDataAccessException - if the query does not return exactly one row, or does not return exactly one column in that row 
DataAccessException - if there is any problem executing the query
See Also:
JdbcOperations.queryForInt(String, Object[])



****************************************************************


 <String> String org.springframework.jdbc.core.JdbcTemplate.queryForObject(String sql, Object[] args, Class<String> requiredType) throws DataAccessException






queryForObject
public <T> T queryForObject(String sql,
                            Object[] args,
                            Class<T> requiredType)
                 throws DataAccessException
Description copied from interface: JdbcOperations 
Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result object. 
The query is expected to be a single row/single column query; the returned result will be directly mapped to the corresponding object type. 




Specified by:
queryForObject in interface JdbcOperations
Parameters:
sql - SQL query to execute
args - arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also contain SqlParameterValue objects which indicate not only the argument value but also the SQL type and optionally the scale
requiredType - the type that the result object is expected to match 
Returns:
the result object of the required type, or null in case of SQL NULL 
Throws: 
IncorrectResultSizeDataAccessException - if the query does not return exactly one row, or does not return exactly one column in that row 
DataAccessException - if the query fails
See Also:
JdbcOperations.queryForObject(String, Class)
****************************************************************************

package com.venkat;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.venkat.JdbcDao.JdbcDaoImpl;


public class JDBCDemo {


/**
* @param args
* @throws Exception 
*/
public static void main(String[] args)   {
// TODO Auto-generated method stub

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
 JdbcDaoImpl dao=context.getBean("jdbcDaoImpl",JdbcDaoImpl.class);

 System.out.println(dao.getStudentCount());
 System.out.println(dao.getStudentName(121));
}


}
************************************************************************
package com.venkat.JdbcDao;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class JdbcDaoImpl {
private DataSource datasource;
private JdbcTemplate jdbcTemplate;

public DataSource getDatasource() {
return datasource;
}


public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}


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

@Autowired
public void setDatasource(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}

public int getStudentCount()
{
String sql="select count(*) from STUDENT";
return jdbcTemplate.queryForInt(sql);
}

public String getStudentName(int studentid)
{
String sql="select studentname from STUDENT where studentid=?";
return jdbcTemplate.queryForObject(sql, new Object[]{studentid}, String.class);
}
}
**************************************************************************
package com.venkat.model;

public class Student {
private int studentid;
private String studentname;
private int studentrank;
public int getStudentid() {
return studentid;
}
public void setStudentid(int studentid) {
this.studentid = studentid;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getStudentrank() {
return studentrank;
}
public void setStudentrank(int studentrank) {
this.studentrank = studentrank;
}
public Student(int studentid, String studentname, int studentrank) {
super();
this.studentid = studentid;
this.studentname = studentname;
this.studentrank = studentrank;
}

}
************************************************************************
<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config/>

<context:component-scan base-package="com.venkat"></context:component-scan>



<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<property name="username" value="venkat"></property>
<property name="password" value="venkat"></property>
</bean></beans>








No comments:

Post a Comment