DAO-Spring MVC : error to execute an update query

611 Views Asked by At

I made a form with Spring MVC. I want use it to fill data in my databases. I use a DAO. With a sysout I show data in the consol but it's impossible to execute the query.

package controlleurs;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Repository("RepertoryDAO")
public class RepertoryDAO {


    private JdbcTemplate myJdbc;
    public RepertoryDAO(){
    }

    @Autowired
    public void setMonJdbc(DataSource ds) {
        //this.JdbcTemplate = new JdbcTemplate(ds);
        this.myJdbc = new JdbcTemplate(ds);
    }


    //Display data
    public List<Repertory> getRepetories(){


        return myJdbc.query("select*from main",new RowMapper<Repertory>(){

            public Repertory mapRow(ResultSet rs, int rowNum1) 
                    throws SQLException {

                Repertory rp=new Repertory();
                rp.setId(rs.getInt("id"));
                rp.setName(rs.getString("name"));
                return rp;
            }
        });
    }

    //Add a repertory in database
    public void addRepertory(String name, String url, int bl, int noFree, String topic){

        String backlinkRequired=Integer.toString(bl);

        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("name", name);
        System.out.println(name);
        params.addValue("url", url);
        System.out.println(url);
        params.addValue("lienRetour", backlinkRequired);
        System.out.println(backlinkRequired);
        params.addValue("payant", noFree);
        System.out.println(noFree);
        params.addValue("topic", topic);
        System.out.println(topic);

        System.out.println("jdbc"+ getMonJdbc().toString());
        this.myJdbc.update("insert into main (name,url,lienRetour,payant,topic) values (:name,:url,:lienRetour,:payant,:topic)",params);
    }

    public JdbcTemplate getMonJdbc() {
        return this.myJdbc;
    }

This line give me anything in the console.

System.out.println("jdbc"+ getMonJdbc().toString());

The console display this error:

déc. 28, 2016 2:58:32 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [MyServletSpring] in context with path [/monProjetSpring] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at controlleurs.RepertoryDAO.addRepertory(RepertoryDAO.java:65) at controlleurs.NewDirectory.directoryRegistered(NewDirectory.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

Thanks for your help.

1

There are 1 best solutions below

0
On BEST ANSWER

It seems to be a basic Spring issue.

  1. First, look if you've got a dataSource registered, for me the autowired annotation just try to inject the dataSource parameter. If it's right, you could try to define a jdbcTemplate bean in your xml config file with the datasource as properties.

  2. Then you can put your @Autowired just above your jdbcTemplate definition in your DAO class.

This may not be the way you want but it could work easily.