简体   繁体   中英

Slick 3 - SQL Plain - MySQL Date

I'm trying to insert a simple java.util.Date into MySQL TIMESTAMP field slick 3 and plain query.

sqlu"""INSERT INTO Table
     (name, date)
  VALUES
     (${obj.name}, ${obj.date})"""

It works perfectly when I just insert the String value.

I found many explanations of an implicit conversion and put:

implicit val JavaUtilDateMapper =
  MappedColumnType .base[java.util.Date, java.sql.Timestamp] (
    d => new java.sql.Timestamp(d.getTime),
    d => new java.util.Date(d.getTime))

sqlu"""INSERT INTO Table
     (name, date)
  VALUES
     (${obj.name}, ${obj.date})"""
db.run(sqlu)

But I'm still getting error: Error:(57, 5) could not find implicit value for parameter e: slick.jdbc.SetParameter[java.util.Date] sqlu"""INSERT INTO Table...

What should I do?

Thank you

Instead of providing the implicit conversion from java.util.Date to java.sql.Timestamp and insert java.sql.Timestamp directly

sqlu"""INSERT INTO Table
     (name, date)
  VALUES
     (${obj.name}, ${new Timestamp(obj.date.getTime)})"""

db.run(sqlu)

or Provide scala implicit def

implicit def toSQLTime(date: java.util.Date): java.sql.Timestamp = new java.sql.Timestamp(date.getTime)

 sqlu"""INSERT INTO Table
         (name, date)
      VALUES
         (${obj.name}, ${obj.date: java.sql.Timestamp})"""

    db.run(sqlu)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM