Announcement Announcement Module
No announcement yet.
need 'extends JdbcDaoSupport' help Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • need 'extends JdbcDaoSupport' help

    hello all,

    i have been trying to read everything i can find on the spring framework but havent had much luck finding tutorials or examples for connecting to a mysql server.

    the one book i have bought and read is o'reillys 'spring - a developers notebook' and they cover jdbc in ch 4, although i am afraid their example requires using hibernate, which i am not using.

    i would appreciate any links to tutorials, info, or anything that might be of help here.

    to see if i am on the right track, here is my setup:

    i extended the class JdbcDaoSupport with my class that actually makes the queries.
    for my GET METHOD: one method returns a list of account objects stored in the this method, i create an ArrayList and JdbcTemplate and call setDataSource() on it which gets its DataSource parameter from a bean.
    i then create an inner class which implements RowCallbackHandler and overwrites the processRow() method. I then call the query() method on my JdbcTemplate which takes a sql command and a new instance of my inner class. Inside my overwritten method in my inner class i create and assign variables to the object that i add to my arraylist. the arraylist is then returned.
    for my SAVE METHOD: it takes in the object i want to save as a parameter. once again create a new JdbcTemplate <side note> what is the best syntax '= getJdbcTemplate() or '= new JdbcTemplate()'? </side note> and call the execute() method which contains a sql command INSERT and extracts values from my object to save.

    is extending JdbcDaoSupport the best/only/worst way to do this? any other ideas or help?

    thanks in advance,
    vincent disco

  • #2
    I find extending JdbcDaoSupport is the best way to do this:
    - Already has setDataSource implemented
    - Implements get/setJdbcTemplate which gives you a chance to either set your own (ie for setting an exception translator) in the config or just set the datasource and let the class create it for you.
    - Implements initDao which is automatically called. Useful for creating any of the object based support classes (ie StoredProcedure).

    Given your example it would look something like the following. This is not pseudo code. It isn't missing any properites. Its assumed the object will be created by using setDataSource or setJdbcTemplate. The JdbcTemplate will be created for me.

    What I changed from your description:
    - Instead of a RowCallbackHandler I used a RowMapper. The JdbcTemplate will take care of creating the list for me.
    - Instead of an inner class I create one instance of an anonymous class. RowMapper is simple enough, an anonymous class works well.
    - Left JdbcTemplate and datasource handling to the super class.

    public class MyDao extends JdbcDaoSupport {
      public MyDao() {
      private RowMapper accountMapper = new RowMapper() {
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
          Account result = new Account();
          return result;
      public List getAccounts(String param) {
        final String sql = "SELECT * from ACCOUNT where param = ?";
        return getJdbcTemplate().query(sql, new Object[] { param }, accountMapper);
      public void saveAccount(Account account) {
        final String sql = "INSERT into ACCOUNT (name, company, numEmployees) values (?, ?, ?)";
        int insertCount = getJdbcTemplate().update(sql, 
          new Object[] {account.getName(), account.getCompany(), new Integer(account.getNumEmployees())}
        System.out.println(insertCount + " rows inserted");
    And my sample Account class
    public class Account {
      private String name;
      private String company;
      private int numEmployees;
      public int getNumEmployees() {
        return numEmployees;
      public String getCompany() {
        return company;
      public void setName(String name) { = name;
      public void setNumEmployees(int numEmployees) {
        this.numEmployees = numEmployees;
      public void setCompany(String company) { = company;
      public String getName() {
        return name;


    • #3
      thanks for quick reply

      appreciate the reply, and thank you for the code sample....analyzing different examples always helps me understand what is going on better.