使用PostgreSQL Scala SBT Intellij IDEA

时间:2017-11-11 22:47:21

标签: postgresql scala intellij-idea slick

我正在尝试使用带有PostgreSQL驱动程序的Intellij IDEA中的Slick创建项目。但我设法只找到this tutorial 我跟着它,但我有一个错误: 线程“main”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"
)

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

1 个答案:

答案 0 :(得分:3)

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

targetSdkVersion

build.sbt

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)
  }
}