【问题标题】:Slick with PostgreSQL Scala SBT Intellij IDEASlick 与 PostgreSQL Scala SBT Intellij IDEA
【发布时间】:2018-04-24 21:53:59
【问题描述】:

我正在尝试使用 PostgreSQL 驱动程序在 Intellij IDEA 中使用 Slick 创建一个项目。但我只找到了this tutorial 我跟着它,但我有一个错误: 线程“主”java.lang.ExceptionInInitializerError 中的异常 在 Main.main(Main.scala) 原因:com.typesafe.config.ConfigException$Missing:未找到密钥“url”的配置设置

这是我的主类代码:

import scala.slick.driver.PostgresDriver.simple._

object Main {

  case class Song(
                   id: Int,
                   name: String,
                   singer: String)

  class SongsTable(tag: Tag) extends Table[Song](tag, "songs") {
    def id = column[Int]("id")

    def name = column[String]("name")

    def singer = column[String]("singer")

    def * = (id, name, singer) <> (Song.tupled, Song.unapply)
  }

  lazy val songsTable = TableQuery[SongsTable] 
  val db = Database.forConfig("scalaxdb")

  def main(args: Array[String]): Unit = {

    val connectionUrl = "jdbc:postgresql://localhost/songs?user=postgres&password=postgresp"

    Database.forURL(connectionUrl, driver = "org.postgresql.Driver") withSession {
      implicit session =>
        val songs = TableQuery[SongsTable]
        songs.list foreach { row =>
          println("song with id " + row.id + " has name " + row.name + " and a singer is " + row.singer)
        }
    }
  }
}

这些是 application.conf 文件:

scalaxdb = {
  dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
  properties = {
    driver = "org.postgresql.Driver"
    url = "jdbc:postgresql://localhost/dbname?user=user&password=password"
  }
}

这是 build.sbt:

libraryDependencies ++= Seq(
  "org.postgresql" % "postgresql" % "9.3-1100-jdbc4",
  "com.typesafe.slick" %% "slick" % "2.1.0",
  "org.slf4j" % "slf4j-nop" % "1.6.4"
)

我无法弄清楚我做错了什么。对于解决此问题的任何建议,我将不胜感激。

【问题讨论】:

    标签: postgresql scala intellij-idea slick


    【解决方案1】:

    您不介意使用更新版本的 Slick 吗?

    import slick.jdbc.PostgresProfile.api._
    import scala.concurrent.Await
    import scala.concurrent.ExecutionContext.Implicits.global
    import scala.concurrent.duration._
    
    object Main {
    
      case class Song(
                       id: Int,
                       name: String,
                       singer: String)
    
      class SongsTable(tag: Tag) extends Table[Song](tag, "songs") {
        def id = column[Int]("id")
    
        def name = column[String]("name")
    
        def singer = column[String]("singer")
    
        def * = (id, name, singer) <> (Song.tupled, Song.unapply)
      }
    
      val db = Database.forConfig("scalaxdb")
    
      val songs = TableQuery[SongsTable]
    
      def main(args: Array[String]): Unit = { 
        Await.result({
          db.run(songs.result).map(_.foreach(row =>
            println("song with id " + row.id + " has name " + row.name + " and a singer is " + row.singer)))
        }, 1 minute)
      }
    }
    

    build.sbt

    scalaVersion := "2.12.4"
    
    libraryDependencies += "com.typesafe.slick" %% "slick" % "3.2.1"
    libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.25"
    libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % "3.2.1"
    libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"
    

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 2018-05-07
      • 2013-09-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 2017-08-15
      • 2017-02-24
      • 2015-06-25
      相关资源
      最近更新 更多