Delete a record in MYSQL using playframework and Form<T> -
hi new play framework.i implement crud operation using mysql database in play framework.
i have able delete, update entire table, not able delete single record. want pass values html.
index.scala.html @(message: string) @main("welcome play") { <ul id="bars"> </ul> <form action="@routes.application.addbar()" method="post"> <label for="name"> enter name</label> <input name="name"/> <br> <label for="place"> enter place</label> <input name="place"/> <br> <input type="submit"/> </form> <form action="@routes.application.getbars()" method="get"> <label for="retrieve"> retrieve details table </label> <input type="submit"/> </form> <form action="@routes.application.deletebar()" method="get"> <label for="name"> enter name deleted</label> <input name="name"/> <br> <input type="submit" value="delete single record"/> </form> <form action="@routes.application.updatebar()" method="post"> <label for="name"> enter name</label> <input name="name"/> <br> <label for="place"> enter place</label> <input name="place"/> <br> <input type="submit"/> </form> }
application.java
package controllers; import com.avaje.ebean.ebean; import com.avaje.ebean.sqlupdate; import java.util.list; import models.bar; import play.*; import play.data.form; import play.db.ebean.model; import play.mvc.*; import play.twirl.api.content; import views.html.*; import play.libs.json; public class application extends controller { public static result index() { int rowcount1 = bar.find.findrowcount(); return ok(index.render("your new application ready.")); } public static result addbar() { bar bar = form.form(bar.class).bindfromrequest().get(); bar.save(); return redirect(routes.application.index()); } public static result login() { bar bar = form.form(bar.class).bindfromrequest().get(); bar.save(); return redirect(routes.application.index()); } public static result getbars(){ list<bar> bars = new model.finder(string.class, bar.class).all(); return ok(json.tojson(bars)); } public static result deletebar(string name) { sqlupdate down = ebean.createsqlupdate("delete bar name = :param1 "); down.setparameter("param1", bar.name); int deletedcount = down.execute(); return ok("deleted " + deletedcount + " record(s) " + bar.name); } }
route below:
get / controllers.application.index() post /bars2 controllers.application.addbar() /bars controllers.application.getbars() /bars1 controllers.application.deletebars() /updatebar1/ controllers.application.login() # map static resources /public folder /assets url path /assets/*file controllers.assets.at(path="/public", file)
what is:
- i using direct sql query delete, want know how can achieve same form class mapping models - data validation also. please provide pseudo code... went through below stuff, no luck
you don't need use form deleting records - for? need identifier or param search for, have 2 ways - first showing sqlupdate
second ebean's method:
list<bar> bars = bar.find.where().eq("name", name).findlist(); (bar bar : bars) { bar.delete(); }
if want delete single record @id
field can (if you're sure bar id exists):
bar.find.ref(barid).delete();
Comments
Post a Comment