How to store date into Mysql database with play framework in scala?

Posted by Rahul Kulhari on Stack Overflow See other posts from Stack Overflow or by Rahul Kulhari
Published on 2013-10-31T06:37:59Z Indexed on 2013/11/06 3:55 UTC
Read the original article Hit count: 248

I am working with play framework with scala and

what am i doing :

  1. login page to login into web app
  2. sign up page to register into web app
  3. after login i want to store all databases values to user

what i want to do:

when user register for web app then i want to store user values into database with current time and date but my form is giving error.

error:

List(FormError(dates,error.required,List())),None)

controllers/Application.scala

object Application extends Controller {

  val ta:Form[Keyword] = Form(
          mapping(
    "id" -> ignored(NotAssigned:Pk[Long]),
    "word" -> nonEmptyText,
    "blog" -> nonEmptyText,
    "cat" -> nonEmptyText,
    "score"-> of[Long],
    "summaryId"-> nonEmptyText,
    "dates" -> date("yyyy-MM-dd HH:mm:ss")
  )(Keyword.apply)(Keyword.unapply)
  )

  def index = Action {
    Ok(html.index(ta));
  }

def newTask=  Action { implicit request =>
  ta.bindFromRequest.fold(
    errors => {println(errors) 
      BadRequest(html.index(errors))},
    keywo => {

      Keyword.create(keywo)
      Ok(views.html.data(Keyword.all()))
    }
  )
}

models/keyword.scala

case class Keyword(id: Pk[Long],word: String,blog: String,cat: String,score: Long, summaryId: String,dates: Date )

object Keyword {

 val keyw = {
    get[Pk[Long]]("keyword.id") ~
     get[String]("keyword.word")~
    get[String]("keyword.blog")~
    get[String]("keyword.cat")~
    get[Long]("keyword.score") ~ 
    get[String]("keyword.summaryId")~
    get[Date]("keyword.dates") map {
    case id~blog~cat~word~score~summaryId~dates => Keyword(id,word,blog,cat,score, summaryId,dates)
  }
}
  def all(): List[Keyword] = DB.withConnection { implicit c =>
  SQL("select * from keyword").as(Keyword.keyw *)
}

def create(key: Keyword){DB.withConnection{implicit c=> 
  SQL("insert into keyword values({word},{blog}, {cat}, {score},{summaryId},{dates})").on('word-> key.word,'blog->key.blog,
      'cat -> key.cat,
      'score-> key.score,
      'summaryId -> key.summaryId,
      'dates->new Date()).executeUpdate
}
}

views/index.scala.html

@(taskForm: Form[Keyword])

@import helper._

@main("Todo list") {



    @form(routes.Application.newTask) {


    @inputText(taskForm("word"))
        @inputText(taskForm("blog"))
        @inputText(taskForm("cat"))
        @inputText(taskForm("score"))
        @inputText(taskForm("summaryId")) 

        <input type="submit">

        <a href="">Go Back</a>
    }


}

please give me some idea to store date into mysql databse and date is not a field of form

© Stack Overflow or respective owner

Related posts about mysql

Related posts about scala