I can not insert records in function postgresql with jdbc

0

Good morning friends, I have a problem when I want to insert records from a call to a postgresql function that receives 2 parameters and I want to do it from Java Server Faces and JDBC this is my java code:

DAO:

 @Override
    public boolean insertPelicula(moviesDTO movies) {
        System.out.println("llllllllllllllll");
        try {

            CallableStatement prcProcedimientoAlmacenado = conn.getConnection().prepareCall("{call sp_insert_update_peliculas(?,?)}");
            prcProcedimientoAlmacenado.setInt(1, movies.getId_movie());
            prcProcedimientoAlmacenado.setString(2, movies.getMovie_name());
            prcProcedimientoAlmacenado.setInt(3, movies.getMovie_year());

            System.out.println("Executando Store Procedure...");
            prcProcedimientoAlmacenado.execute();
            return true;
        } catch (SQLException ex) {
            System.out.println(ex);
            return false;
        }

    }

This is my bean code:

Note: Edit the bean part:

     /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package avo.ob.movie;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;

/**
 *
 * @author phoenix
 */
@ManagedBean
@ViewScoped
public class MovieBean implements Serializable {

    private List<moviesDTO> peliculasList = new ArrayList<>();
    private List<moviesDTO> peliculasFitradas = new ArrayList<>();
    private String nombrePelicula = "";
    private Integer anyo;
    private MoviesDAO dao = new MovieDAOImplements();
    moviesDTO movies = new moviesDTO();

    //private MoviesDAO moviesDAO;
    @PostConstruct
    public void init() {
        peliculasList = new MovieDAOImplements().getMovies();

        //peliculasFitrados = peliculasList;
        peliculasFitradas = new ArrayList<>();
        peliculasFitradas.addAll(peliculasList);
        System.out.println("Peliculas filtradas..." + peliculasFitradas.size());

    }

    public void buscarpelicula() {
        peliculasFitradas.clear();
        for (moviesDTO item : peliculasList) {

            if (item.getMovie_name().toLowerCase().contains(nombrePelicula.toLowerCase())) {
                peliculasFitradas.add(item);
            }

        }
        System.out.println("Filtor..." + peliculasFitradas.size());

    }

    public void insertarPelicula() {

        if (movies != null) {
            System.out.println("Que onda con el insert..." + movies.getMovie_name());


            dao.insertPelicula(movies);
            //System.out.println("Que pedo con el insert..." + movies.getId_movie());


        }

    }

    public List<moviesDTO> getPeliculasList() {
        return peliculasList;
    }

    public void setPeliculasList(List<moviesDTO> peliculasList) {
        this.peliculasList = peliculasList;
    }

    public String getNombrePelicula() {
        return nombrePelicula;
    }

    public void setNombrePelicula(String nombrePelicula) {
        this.nombrePelicula = nombrePelicula;
    }

    public List<moviesDTO> getPeliculasFitradas() {
        return peliculasFitradas;
    }

    public void setPeliculasFitrados(List<moviesDTO> peliculasFitradas) {
        this.peliculasFitradas = peliculasFitradas;
    }

    public Integer getAnyo() {
        return anyo;
    }

    public void setAnyo(Integer anyo) {
        this.anyo = anyo;
    }

    public MoviesDAO getDao() {
        return dao;
    }

    public void setDao(MoviesDAO dao) {
        this.dao = dao;
    }

}

And finally the one in the view, here I am using Java Server Faces, I would only put the component of the button since the insert is called:

 <h:panelGroup>
                    <p:commandButton value="Guardar Pelicula" update="data-table" actionListener="${movieBean.insertarPelicula}" process="@this"/>
                </h:panelGroup>

New note: I also put my post SP SP of postgresql:

-- FUNCTION: public.sp_insert_update_peliculas(character varying, integer)

-- DROP FUNCTION public.sp_insert_update_peliculas(character varying, integer);

CREATE OR REPLACE FUNCTION public.sp_insert_update_peliculas(
    out_nombrepelicula character varying,
    out_aniopelicula integer)
    RETURNS TABLE(mensaje character varying, success boolean) 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$

DECLARE
var_mensaje character varying;
var_success boolean;
var_max_idmovie int;
var_n integer;
BEGIN
        var_max_idmovie := (SELECT MAX(id_movie)+1 
        FROM cat_movies);
        INSERT INTO cat_movies(
            id_movie,
            movie_name,
            movie_year)
            VALUES (var_max_idmovie,
                    $1,
                    $2);        
        GET DIAGNOSTICS var_n= ROW_COUNT;
                GET DIAGNOSTICS var_n = ROW_COUNT;
                IF(var_n>0)THEN
                    var_success=true;
                    var_mensaje='Registro agregado satisfactoriamente.';
                ELSE
                    var_success=false;
                    var_mensaje='Error en el sistema favor de contactar a su administrador.';




END IF;
    RETURN QUERY SELECT var_mensaje,var_success;
END;

--select max(id_movie) from cat_movies

$BODY$;

ALTER FUNCTION public.sp_insert_update_peliculas(character varying, integer)
    OWNER TO postgres;

I explain it briefly here basically it is getting 2 parameters that is the name and the year of the movie and the id of the film autoincrements looking for the maximum of the id_movie and it is added to a variable and later it is used so that it is inserted, I'm not sure if you also have to say it from java, and if so how it could be put, because it is possibly what is giving the null, although in theory should not do that since the sp already has it.

The error that is thrown is a nullpointerException at the moment of clicking on the "Save Movie" button I leave the error code:

29-Oct-2018 11:04:44.699 GRAVE [http-nio-8080-exec-254] com.sun.faces.context.AjaxExceptionHandlerImpl.handlePartialResponseError java.lang.NullPointerException
    at avo.ob.movie.MovieDAOImplements.insertPelicula(MovieDAOImplements.java:70)
    at avo.ob.movie.MovieBean.insertarPelicula(MovieBean.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Que onda con el insert...null

I do not know what may be causing the error, although I know what it is: I would be coming to an object that is null and that is why it sends that exception, I hope and you can help me with this problem, I remain Pending friends, greetings.

    
asked by cratus666jose 29.10.2018 в 18:08
source

0 answers