使用Slick 3.0连接到Mysql-没有用户名,没有密码和伪造的驱动程序不等于错误 [英] Connecting to Mysql using Slick 3.0 - No username, no password and bogus driver does not equal error

查看:152
本文介绍了使用Slick 3.0连接到Mysql-没有用户名,没有密码和伪造的驱动程序不等于错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个非常简单的scala脚本,以使用slick 3连接到Mysql.

I'm writing a veery simple scala script to connect to Mysql using slick 3.

我的 build.sbt 看起来像这样:

name := "slick_sandbox"

version := "1.0"

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "com.typesafe.slick" %% "slick" % "3.0.3",
  "org.slf4j" % "slf4j-nop" % "1.6.4",
  "mysql" % "mysql-connector-java" % "5.1.6"

)

application.conf :

Drivder是故意的错误;另外,我没有提供数据库用户名密码!

Drivder is an intentional mistake; also, I did not provide a db username or password!

mysqldb = {
  url = "jdbc:mysql://localhost/slickdb"
  driver = com.mysql.jdbc.Drivder
  connectionPool = disabled
  keepAliveConnection = true
}

Main.scala

导入slick.driver.MySQLDriver.api._ 导入scala.concurrent.ExecutionContext.Implicits.global

import slick.driver.MySQLDriver.api._ import scala.concurrent.ExecutionContext.Implicits.global

object Main {

  def main(args: Array[String]) {

    // test to see this function is being run; it IS
    println("foobar")

    // I expected an error here due to the intentional
    // mistake I've inserted into application.conf
    // I made sure the conf file is getting read; if I change mysqldb
    // to some other string, I get correctly warned it is not a
    // valid key
    val db = Database.forConfig("mysqldb")

    val q = sql"select u.name from users ".as[String]

    db.run(q).map{ res=>
      println(res)
    }
  }
}

它可以编译.现在是我在终端上运行sbt run时看到的结果:

It compiles OK. Now this is the result I see when I run sbt run on the terminal:

felipe@felipe-XPS-8300:~/slick_sandbox$ sbt run
[info] Loading project definition from /home/felipe/slick_sandbox/project
[info] Set current project to slick_sandbox (in build file:/home/felipe/slick_sandbox/)
[info] Compiling 1 Scala source to /home/felipe/slick_sandbox/target/scala-2.11/classes...
[info] Running Main 
foobar
[success] Total time: 5 s, completed Sep 17, 2015 3:29:39 AM

一切看上去都看似不错;即使我在不​​存在的数据库上显式运行查询,slick仍然继续进行,好像什么都没发生.

Everything looks deceptively OK; even though I explicitly ran the query on a database that doesn't exist, slick went ahead as if nothing has happened.

我在这里想念什么?

推荐答案

Slick异步运行查询.因此,它只是没有足够的时间来执行它.就您而言,您必须等待结果.

Slick runs queries asynchronously. So it just didn't have enough time to execute it. In your case you have to wait for result.

object Main {

  def main(args: Array[String]) {

    println("foobar")

    val db = Database.forConfig("mysqldb")

    val q = sql"select u.name from users ".as[String]

    Await.result(
      db.run(q).map{ res=>
      println(res)
    }, Duration.Inf)
  }
}

这篇关于使用Slick 3.0连接到Mysql-没有用户名,没有密码和伪造的驱动程序不等于错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆