Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:07:16+00:00 2026-05-26T21:07:16+00:00

I’m a Spring newbie, I’m following some examples from books and tutorials in the

  • 0

I’m a Spring newbie, I’m following some examples from books and tutorials in the spring website and I can’t get my code to work.

I’m trying to access to a DataBase using JdbcTemplate. This is how my root-context.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/db" />
    <property name="username" value="userr" />
    <property name="password" value="password" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
    <constructor-arg ref="dataSource"/>
</bean>
<bean id="myDao" class="com.me.myproj.persistence.JdbcMyaDao">
    <property name="dataSource" ref="dataSource"/>
</bean>

This is my JdbcMyDao (all imports are ok, I won’t paste them):

public class JdbcMyDao implements MyDao{

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
 }

public String getOpcionById(){
    String SQL_Q="select name from options where id=35";

    return (String)this.jdbcTemplate.queryForObject(SQL_Q, String.class);
}

}

Finally, my controller:

@Controller
public class myController {

    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(Model model){

        JdbcMyDao daoP=new JdbcMyDao();
        String op=daoP.getOpcionById();

        model.addAttribute("op",op);
        return "show";
    }
}

Ok, then, show.jsp just prints the return string.

So, this is a very simple access to a DataBase, but I get a nullPointerException when using JdbcTemplate (in function getOpcionById)

I think this is because function setDataSource is never runned, I thought spring runs it by IoC, I tried to put @Autowired to jdbcTemplate property i JdbcMyDao, but it didn’t work either. Can someone tell me the steps Spring does and which steps should I do? Or what changes should I make and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:07:17+00:00Added an answer on May 26, 2026 at 9:07 pm

    Your controller is instantiating its own JdbcMyDao and calling that, rather than using the bean you declared in root-context.xml. You need to inject the myDao bean into your controller and invoke that.

    Also, is there a reason you’re defining a JdbcTemplate bean, and then not doing anything with it? The setDataSource method of the DAO is instantiating its own JdbcTemplate (which is fine), so there’s no reason to declare a bean.

    I have a feeling you’ve not quite grasped the concept of dependency injection – you seem to have used direct instantiation and declared beans, rather than just the latter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.