Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: datasource Integration

  1. #1
    Join Date
    Sep 2010
    Posts
    10

    Default datasource Integration

    I cant manage it to use simpleJdbcDaoSupport in my Project.
    I always get a NullPointerException, because he cant connect to my datasource. Can anybody help me plz?

    DbConnectionDAO:
    Code:
    public class DbConnectionDAO extends SimpleJdbcDaoSupport implements DbConnectionImplDAO{
    
        public List<Hotel> getHotels(){
            logger.info("Getting Hotels");
            return getSimpleJdbcTemplate().query("SELECT * FROM hotels", new RowMapper<Hotel>(){
                public Hotel mapRow(ResultSet rs, int i) throws SQLException {
                    return new Hotel(rs.getString(1), rs.getString(2), rs.getString(3));
                }
            });
            
        }
    }
    applicationContext:
    Code:
    <?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:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/hotels/" />
            <property name="username" value="root" />
            <property name="password" value="test1234" />
        </bean>
        <bean id="dbConnectionDAO" class="dao.DbConnectionDAO">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    </beans>
    IndexController:
    Code:
    public class IndexController implements Controller{
        private DbConnectionDAO dbConn = new DbConnectionDAO();
        protected final Log logger = LogFactory.getLog(getClass());   
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
            ModelAndView mv = new ModelAndView("index");        
            List<Hotel> hotelList = dbConn.getHotels();
            mv.addObject("hotelList",hotelList);      
    
            return mv;
        }
    }

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You are creating your own instance of the dao in the controller instead of using the one from the applicationcontext. You should also dependency inject your dao.

    Code:
    public class IndexController implements Controller{
        private DbConnectionDAO dbConn = new DbConnectionDAO();
        protected final Log logger = LogFactory.getLog(getClass());   
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
            ModelAndView mv = new ModelAndView("index");        
            List<Hotel> hotelList = dbConn.getHotels();
            mv.addObject("hotelList",hotelList);      
    
            return mv;
        }
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Sep 2010
    Posts
    10

    Default

    Thank you! This works!

    Did you mean it like this or?

    Thought, that its enough to put the applicationContext path into my web.xml and he will get it on himself...

    Code:
    public class IndexController implements Controller{
        private ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("../applicationContext.xml");
        protected final Log logger = LogFactory.getLog(getClass());   
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
            DbConnectionDAO dbConn = (DbConnectionDAO)ctx.getBean("DbConnectionDAO");
            ModelAndView mv = new ModelAndView("index");
            List<Hotel> hotelList = dbConn.getHotels();
            mv.addObject("hotelList",hotelList);      
    
            return mv;
        }
    }

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    If you want to run out of resources (memory, databaseconnections etc.) this is the way to go...

    Simply inject it, like you inject the datasource into your dao. Create a setter and add the necessary configuration
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Sep 2010
    Posts
    10

    Default

    Okok.. i see

    Why is my dao NULL here? Probably setDao is called after handleRequest(), but how can i change this?
    When i try to inject it through the constructor, i got always told that i dont have a default constructor defined, but if i do.. it doesnt work either..

    Code:
    public class IndexController implements Controller{
        protected final Log logger = LogFactory.getLog(getClass());   
        private DbConnectionDAO dao;
    
        public void setDao(DbConnectionDAO dao){
            this.dao = dao;
        }
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
            ModelAndView mv = new ModelAndView("index");
            logger.error("DAO "+dao);
            List<Hotel> hotelList = dao.getHotels();
            mv.addObject("hotelList",hotelList);      
    
            return mv;
        }
    
        
    }
    Code:
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/hotels" />
            <property name="username" value="root" />
            <property name="password" value="test1234" />
        </bean>
        <bean id="DbConnectionDAO" class="dao.DbConnectionDAO">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <bean id="IndexController" class="Controller.IndexController">
            <constructor-arg><ref bean="DbConnectionDAO"/></constructor-arg>
        </bean>

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I suggest you start by looking at the samples. You seem to be missing some fundamentle concepts here.

    Only adding a setter isn't enough, you will have to wire it also.

    Code:
    <bean id="IndexController" class="Controller.IndexController">
      <property name="dao" ref="DbConnectionDAO" />
    </bean>
    If you want to use the constructor you will have to ADD that to your controller.

    Code:
    public IndexController(DbConnectionDao dao) {
      super();
      this.dao=dao;
    }
    If you get the exception you probably have a duplicate configuration propably also for the Servlet. If so remove the indexcontroller from the root context.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Sep 2010
    Posts
    10

    Default

    Could you suggest me some examples which i could look at?
    The Spring Documentation is so huge.. i dont know where to begin...

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I suggest the index . Sorry just a joke. Just take a look at the parts you need, I suggest chapter 3, explaining the factory/applicationcontext and then take a look at the web chapter.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Sep 2010
    Posts
    10

    Default

    Haha good joke, but a little bit true ;-)

    Ok i take a look at it...

    It's funny to learn a lot of new interesting stuff.

  10. #10
    Join Date
    Sep 2010
    Posts
    10

    Default

    Mh.. i have read now through a lot of the Documentation.
    But i dont get it why my example doesnt work.

    All of the samples i have seen, are exactly like my setter based injection.. and they use the Object afterwards.. but i cant.

    Dont know why i have to autowire it... actually when i try to autowire it doesnt work also.. maybe i do it wrong.. but mh..

    Can you please help me out?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •