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:
applicationContext: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)); } }); } }
IndexController: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>
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; } }


Reply With Quote
. 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.
