91-9990449935 0120-4256464 |
ResultSetExtractor Example | Fetching Records by Spring JdbcTemplateWe can easily fetch the records from the database using query() method of JdbcTemplate class where we need to pass the instance of ResultSetExtractor. Syntax of query method using ResultSetExtractorResultSetExtractor InterfaceResultSetExtractor interface can be used to fetch records from the database. It accepts a ResultSet and returns the list. Method of ResultSetExtractor interfaceIt defines only one method extractData that accepts ResultSet instance as a parameter. Syntax of the method is given below: Example of ResultSetExtractor Interface to show all the records of the tableWe are assuming that you have created the following table inside the Oracle10g database. Employee.javaThis class contains 3 properties with constructors and setter and getters. It defines one extra method toString(). EmployeeDao.javaIt contains on property jdbcTemplate and one method getAllEmployees. applicationContext.xmlThe DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password. There are a property named datasource in the JdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the JdbcTemplate class for the datasource property. Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also. Test.javaThis class gets the bean from the applicationContext.xml file and calls the getAllEmployees() method of EmployeeDao class.
download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE)
Next TopicRowMapper Example
|