如何与MySQL数据库连接Scala.js



如何使用mySQL数据库连接scala.js控制器方法?

这是我到目前为止所拥有的:

case class data(user_d: String, udid: String, image: String)
object mysqlDAO {
  def findById(udid: Long): Option[data] = {
    val connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/umo", "root", "")
    try {
      val statement = connection.prepareStatement("""select * from messages where udid = ?""")
      statement.setLong(1, udid)
      val rs = statement.executeQuery()
      if (rs.next())
        Option(new data(rs.getString(1), rs.getString(2), rs.getString(3)))
      else
        Option.empty
    } finally {
      connection.close()
    }
  }
}
first of all you can create the case class in Model package then u can create object and serialize the your case class Data 

    ----------

    case class Data(user_id: String, id: String, image: String)
    object Data {
    /*   this one is the serialization of the data*/
      implicit val DataWrites = new Writes[Data] {
        def writes(json_write: Data): JsValue = {
          Json.obj(
            "user_id" -> json_write.user_id,
            "id" -> json_write.id,
            "image" -> json_write.image
          )
        }
      }
      val simple = {
        get[String]("user.user_id") ~
          get[String]("user.id") ~
          get[String]("user.image") map {
            case user_id ~ id ~ image =>
              Data(user_id, id, image)
          }
      }
      /*to get the data */
      def find(udid: String): Option[Data] = {
        DB.withConnection { implicit connection =>
          SQL(ConstantSQL.GET_DETAILS).on('uid -> id).as(Data.simple.singleOpt)
        }
      }
    }

    ----------
    //query for to get the data based on the uid
     val GET_DETAILS = """select * from user_image where uid = ?"""
    then u can create your own controller method

    ----------

    var uDetails = Data.find(uid)

    ----------
    use the above line to get the data in your class

最新更新