Can't executeUpdate() in Grails/Groovy web application -
i trying make crud web service psql. achieved while had domain. don't need domain class , started remake this. can create , delete data database when comes edit error:
uri /test/customer/edit/2 class org.codehaus.groovy.runtime.typehandling.groovycastexception message cannot cast object 'null' class 'null' class 'long'. try 'java.lang.long' instead
i have controller
package test class customercontroller { def customerservice def index = { redirect action: "list" } def create() {} def edit () { [customer: customerservice.updateaction(params.id,params.name,params.thl,params.dt1)] } def list() { [customers : customerservice.listaction()] } def save() { println params [customer: customerservice.insertaction(params.id,params.name,params.thl,params.dt1)] redirect action: "list" } def update(){ [customer: customerservice.updateaction(params.id,params.name,params.thl,params.dt1)] redirect action: "list" } def delete(){ [customer: customerservice.deleteaction(params.id)] redirect action: "list" } }
web service
package test import groovy.sql.sql import grails.transaction.transactional @transactional class customerservice { def datasource def listaction(){ def sql = new sql(datasource) return sql.rows ("select * mn") } def insertaction(string id, string name,string thl,string dt1){ def sql = new sql(datasource) sql.execute("insert mn (id, name, thl, dt1) values (${id long},$name,${thl long},$dt1)") } ddef updateaction (string id,string name,string thl,string dt1){ def sql = new sql(datasource) sql.executeupdate("update mn set id=${id long}, name=$name, thl=${thl long}, dt1=$dt1 id=${id long}") } def deleteaction(string id){ def sql= new sql(datasource) sql.execute("delete mn id=${id long}") } }
and gsp
<body> <g:render template="/customer/header"/> <g:render template="/customer/navbar"/> <h1>edit contact</h1> <g:form controller="customer" action="update" method="post"> <div class="container"> <div class="row"> <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2"> <div class="form-group"> <label for="id">id</label> <input type="text" class="form-control" name="id" id="id" placeholder="id"> </div> </div> <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2"> <div class="form-group"> <label for="name">name</label> <input type="text" class="form-control" name="name" id="name" placeholder="name"> </div> </div> <div class="col-lg 2 col-md-2 col-sm-2 col-xs-2"> <div class="form-group"> <label for="thl">thlefvno</label> <input type="text" class="form-control" id="thl" name="thl" placeholder="thlefvno"> </div> </div> </div> <div class="row"> <div class='col-lg 6 col-md-6 col-sm-6 col-xs-6'> <div class="form-group"> <label class="control-label col-sm-2" ="dt1">date :</label> <div class='input-group date' id="dt1"> <input id="dt1" name="dt1" type='text' class="form-control"> <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> </div> </div> <script type="text/javascript"> $('#dt1').datetimepicker({ }); </script> </div> <div class="row"> <div class='col-lg 6 col-md-6 col-sm-6 col-xs-6'> <g:actionsubmit value="update" class="btn btn-info" role="button" /> </div> </div> </div> </g:form> </body> </html>
solution:
service
def getcustomerbyid (long id) { def sql = new sql(datasource) sql.execute("update mn set id=$id id=${id long}") }
controller
def edit () { [customer: customerservice.getcustomerbyid(params.id long)] }
when type /test/customer/edit/2
edit
action customercontroller
called. there no name, thl , dt1 in params object, because didn't provide them in url. there id 2.
trying call customerservice.updateaction
following arguments: customerservice.updateaction(2,null,null,null)
in updateaction
convert thl this: ${thl long}
, tgl null , can't casted long, because long primitive type , can't null. can cast long (this object, objects can null) don't think solution in case.
first of all, should remove casting logic updateaction. if need long instead of string - cast in controller , pass method.
second observation: need execute update in edit action? guess need customer id there , pass gsp:
def edit () { [customer: customerservice.getcustomerbyid(id string)] }
in case should create getcustomerbyid
method
Comments
Post a Comment