nayan
nayan

Reputation: 149

Property 'dataSource' is required Spring JDBC

I tried the code below but it shows the following error

Nested Exception Datasource is required

Spring Tool Suite

Version: 3.7.1.RELEASE

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    <beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <beans:property name="url" value="jdbc:mysql://localhost:3306/mkyong"/>
        <beans:property name="username" value=""/>
        <beans:property name="password" value=""/>
    </beans:bean>
    <beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>
    <context:component-scan base-package="com.tehnocracksolutions.JdbcExample" />
</beans:beans>

Controller

package com.tehnocracksolutions.JdbcExample.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tehnocracksolutions.JdbcExample.Model.CreateModel;

@Controller
public class UserController {

    @RequestMapping(value="create",method=RequestMethod.POST)
    public String createUser(){
        CreateModel model = new CreateModel();
        model.insertdata();
        return "welcome";
    }
}

Model: used mySQL for database connection it seems that datasource are not passed correctly

package com.tehnocracksolutions.JdbcExample.Model;

import javax.sql.DataSource;

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

public class CreateModel {
    @Autowired
    DataSource datasource;

    public void insertdata(){
        JdbcTemplate jdbcTemplate =  new JdbcTemplate(datasource);
        jdbcTemplate.execute("insert into data values('nayan','m','m','t')");
    }

    public DataSource getDatasource() {
        return datasource;
    }

    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
    }
}

Upvotes: 0

Views: 4598

Answers (3)

Ashish Ani
Ashish Ani

Reputation: 332

1st way : Declare data source in your class files as :

org.springframework.jdbc.datasource.DriverManagerDataSource dataSource;

2nd way : Or just declare jdbc template in your CreateModel and autowired it with Jdbc template. Then, you need not to declare DataSource in your CreateModel Class.

Upvotes: 1

Manish Mane
Manish Mane

Reputation: 64

Your configurations are right. Just check that you have created database in mysql named "mkyong".

Upvotes: 0

anand mishra
anand mishra

Reputation: 900

Use configured spring bean datasource and Do not use

import javax.sql.DataSource;

Upvotes: 1

Related Questions