Compdigitec Labs

« | Home | »

Accessing SQL databases from Scala

By admin | July 21, 2021

import scalikejdbc._
import scalikejdbc.scalikejdbcSQLInterpolationImplicitDef

// initialize JDBC driver & connection pool
Class.forName("org.postgresql.Driver")

scalikejdbc.ConnectionPool.singleton("jdbc:postgresql://localhost:5432/", "user", "abcdef888")

sql"""
create table members (
  id serial not null primary key,
  fav_num integer NOT NULL,
  name text NOT NULL
)
""".execute.apply()(scalikejdbc.AutoSession)

sql"""
INSERT INTO members (name, fav_num) VALUES ('Foo', 123)
""".execute.apply()(scalikejdbc.AutoSession)

sql"""
INSERT INTO members (name, fav_num) VALUES ('Bar', 456)
""".execute.apply()(scalikejdbc.AutoSession)

// http://scalikejdbc.org/documentation/operations.html
scalikejdbc.DB.readOnly { implicit session =>
  sql"select * from members".foreach { (rs: scalikejdbc.WrappedResultSet) =>
    println(rs.toMap())
  }
}

You can run the database with Docker (example) as follows:

docker run --name mypsql -e POSTGRES_USER=user -e POSTGRES_PASSWORD=abcdef888 -p 5432:5432 postgres:13.3

Reference instructions for connecting to the docker instance:

psql -h localhost -U user
\dt
select * from members;

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Code | 7 Comments »

7 Responses to “Accessing SQL databases from Scala”

  1. Harry Says:
    August 16th, 2023 at 05:43

    It helped me in my coding thanks.

  2. io games Says:
    October 25th, 2025 at 02:56

    Great article on accessing SQL databases from Scala! The clear code examples make it easy to follow. It almost feels like navigating a well-designed game level. Speaking of games, anyone else enjoy a quick round of io games between coding sessions? They’re a fun way to de-stress. Thanks for sharing this valuable resource!

  3. gay xxx Says:
    December 25th, 2025 at 10:05

    Solid read. I’ve seen similar patterns on new 18+ video sites that prioritize usability

    Also visit my web blog :: gay xxx

  4. https://gay0day.com/videos/109134/epic-large-dongs-vol-three-breaking-in-a-fag-popperstrainer-compilation/ Says:
    December 26th, 2025 at 02:40

    Good discussion. I’ve noticed adult video sites evolve a lot
    in terms of performance and layout

    Here is my website :: https://gay0day.com/videos/109134/epic-large-dongs-vol-three-breaking-in-a-fag-popperstrainer-compilation/

  5. https://zeenite.com/videos/62519/jeune-salope-baise-avec-son-beau-pere-qui-jouie-son-sperme-dans-sa-chatte/ Says:
    December 26th, 2025 at 17:08

    Very true. A smooth interface goes a long way on 18+ video
    sites

    Also visit my web site :: https://zeenite.com/videos/62519/jeune-salope-baise-avec-son-beau-pere-qui-jouie-son-sperme-dans-sa-chatte/

  6. https://thetranny.com/categories/indian/ Says:
    January 3rd, 2026 at 19:33

    Well said. A smooth interface goes a long way on 18+ video sites

    Look into my site :: https://thetranny.com/categories/indian/

  7. https://xlilith.com/search/family-incest/ Says:
    January 3rd, 2026 at 19:37

    Solid perspective. Modern adult platforms feel very distinct from older ones

    My web-site; https://xlilith.com/search/family-incest/

Comments