Question

I am trying to connect to a MSSQL database using the slick framework. The following code shows my first attempt but I can't figure out what is wrong.

This error occurs when leaving it as shown below: [1] value create is not a member of scala.slick.lifted.DDL

Now I delete the line because I do not necessarily need to create the table within my scala code. But then another error arises: [2] value map is not a member of object asd.asd.App.Coffees

package asd.asd

import scala.slick.driver.SQLServerDriver._
import scala.slick.session.Database.threadLocalSession

object App {

object Coffees extends Table[(String, Int, Double)]("COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def supID = column[Int]("SUP_ID")
  def price = column[Double]("PRICE")
  def * = name ~ supID ~ price
}

  def main(args : Array[String]) {
    println( "Hello World!" )

    val db = slick.session.Database.forURL(url = "jdbc:jtds:sqlserver", user = "test", password = "test", driver = "scala.slick.driver.SQLServerDriver")

    db withSession {

      Coffees.ddl.create [1]

//        Coffees.insertAll(
//            ("Colombian",         101, 7.99),
//          ("Colombian_Decaf",   101, 8.99),
//          ("French_Roast_Decaf", 49, 9.99)
//        )

        val q = for {
            c <- Coffees [2]
        } yield (c.name, c.price, c.supID)

        println(q.selectStatement)

        q.foreach { case (n, p, s) => println(n + ": " + p) }
    }
  }

}
Était-ce utile?

La solution

Problem solved. What I did was the following: Update to the latest Slick version and then adjust the code like demonstrated here. Afterwards you need to exchange the line

import scala.slick.driver.H2Driver.simple._

with

import scala.slick.driver.SQLServerDriver.simple._

And modify the connect string to

[...]
Database.forURL("jdbc:jtds:sqlserver://localhost:1433/<DB>;instance=<INSTANCE>", driver = "scala.slick.driver.SQLServerDriver") withSession {
[...]

After this worked out, I decided to use a c3p0 pooled connection (Which makes Slick very much faster and thus first usable, I do highly recommend to use connection pooling!). This left me with the following database object.

package utils

import scala.slick.driver.SQLServerDriver.simple._
import com.mchange.v2.c3p0.ComboPooledDataSource

object DatabaseUtils {
    private val ds = new ComboPooledDataSource
    ds.setDriverClass("scala.slick.driver.SQLServerDriver")
    ds.setUser("supervisor")
    ds.setPassword("password1")
    ds.setMaxPoolSize(20)
    ds.setMinPoolSize(3)
    ds.setTestConnectionOnCheckin(true)
    ds.setIdleConnectionTestPeriod(300)
    ds.setMaxIdleTimeExcessConnections(240)
    ds.setAcquireIncrement(1)
    ds.setJdbcUrl("jdbc:jtds:sqlserver://localhost:1433/db_test;instance=SQLEXPRESS")
    ds.setPreferredTestQuery("SELECT 1")
    private val _database = Database.forDataSource(ds)

    def database = _database
}

You can use this like displayed below.

DatabaseUtils.database withSession {
    implicit session =>
    [...]
}

Last the Maven dependency for c3p0 and the latest Slick version.

<dependency>
  <groupId>com.typesafe.slick</groupId>
  <artifactId>slick_2.10</artifactId>
  <version>2.0.0-M3</version>
</dependency>
<dependency>
  <groupId>com.mchange</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.2.1</version>
</dependency>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top