ScalikeJDBC


Just Write SQL And Get Things Done 💪


This library seamlessly wraps JDBC APIs, offering intuitive and highly flexible functionalities. With QueryDSL, your code becomes inherently type-safe and reusable.

ScalikeJDBC is not just practical; it’s production-ready. Utilize this library confidently in your real-world projects.

Logo


Working on JDBC Layer

Whether you prefer it or not, JDBC stands as a steadfast standard interface. Given its widespread support across most RDBMS, accessing databases remains consistent. We adhere to this standard rigorously, ensuring each release passes through comprehensive unit tests across various RDBMS platforms:

  • PostgreSQL
  • MySQL
  • H2 Database Engine
  • HSQLDB

We firmly believe that ScalikeJDBC seamlessly integrates with a variety of RDBMS, including Oracle, SQL Server, and others. Its robust design ensures compatibility and reliability across different database platforms.

Amazon Redshift, Facebook Presto also supports JDBC

If you can access a datastore via the JDBC interface, you can also seamlessly access it through ScalikeJDBC. For instance, Amazon Redshift and Facebook Presto have added support for the JDBC interface, enabling easy integration with ScalikeJDBC.


Less Dependencies

The core of ScalikeJDBC boasts minimal dependencies, sparing you from the woes of dependency hell. Enjoy a streamlined development experience without unnecessary complications.

  • JDBC Drivers you need
  • Commons DBCP
  • SLF4J API

Certainly, you have the flexibility to utilize c3p0 or other connection pool libraries instead of commons-dbcp with ScalikeJDBC. While ScalikeJDBC doesn’t offer a default ConnectionPool implementation for these alternatives, you have the freedom to integrate them seamlessly according to your project requirements.

No Non-Blocking?

While JDBC drivers inherently block on socket IO, making them potentially unsuitable for async event-driven architectures, it’s worth noting that most real-world applications currently don’t require such architecture. JDBC remains a crucial infrastructure for JVM-based apps.

However, if you’re inclined towards non-blocking database access, consider exploring ScalikeJDBC-Async. This library offers non-blocking APIs for communicating with PostgreSQL and MySQL in a JDBC-like manner.

Please be aware that ScalikeJDBC-Async is still in its alpha stage. If you lack the motivation or resources to investigate and address issues independently, we recommend awaiting the stable version release in the future. You can find more information and the library’s source code on its GitHub page:

https://github.com/scalikejdbc/scalikejdbc-async

FAQ

See also FAQs here: /documentation/faq.html


Getting Started


If you’re looking to execute SQL queries efficiently, the best approach is to use ScalikeJDBC along with the appropriate JDBC driver for your database. Here’s how you can get started quickly!

Depedencies

To get started with ScalikeJDBC, add the following dependency to your build.sbt:

ScalikeJDBC 4.x

ScalikeJDBC 4 requires Java SE 8 or higher. If you still need to run your applications on Java SE 7, keep using ScalikeJDBC 2.5:

// Scala 2.12, 2.13 and 3
libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc"       % "4.3.2",
  "com.h2database"  %  "h2"                % "2.2.224",
  "ch.qos.logback"  %  "logback-classic"   % "1.5.6"
)

ScalikeJDBC 2.x

See ScalikeJDBC 2.x Documentation for details.

// Scala 2.10, 2.11, 2.12
libraryDependencies ++= Seq(
  "org.scalikejdbc" %% "scalikejdbc"               % "2.5.2",
  "com.h2database"  %  "h2"                        % "2.2.224",
  "ch.qos.logback"  %  "logback-classic"           % "1.5.6"
)

Quick Code example


Put the above dependencies into your build.sbt and run sbt console now.

import scalikejdbc._

// initialize JDBC driver & connection pool
Class.forName("org.h2.Driver")
ConnectionPool.singleton("jdbc:h2:mem:hello", "user", "pass")

// ad-hoc session provider on the REPL
implicit val session: DBSession = AutoSession

// table creation, you can run DDL by using #execute as same as JDBC
sql"""
create table members (
  id serial not null primary key,
  name varchar(64),
  created_at timestamp not null
)
""".execute.apply()

// insert initial data
Seq("Alice", "Bob", "Chris") foreach { name =>
  sql"insert into members (name, created_at) values (${name}, current_timestamp)".update.apply()
}

// for now, retrieves all data as Map value
val entities: List[Map[String, Any]] = sql"select * from members".map(_.toMap).list.apply()

// defines entity object and extractor
import java.time._
case class Member(id: Long, name: Option[String], createdAt: ZonedDateTime)
object Member extends SQLSyntaxSupport[Member] {
  override val tableName = "members"
  def apply(rs: WrappedResultSet) = new Member(
    rs.long("id"), rs.stringOpt("name"), rs.zonedDateTime("created_at"))
}

// find all members
val members: List[Member] = sql"select * from members".map(rs => Member(rs)).list.apply()

// use paste mode (:paste) on the Scala REPL
val m = Member.syntax("m")
val name = "Alice"
val alice: Option[Member] = withSQL {
  select.from(Member as m).where.eq(m.name, name)
}.map(rs => Member(rs)).single.apply()

How did it go? If you’d like to know more details or practical examples, see documentation.


Quick Tour


Using only the Scala standard API & SQL

Library users don’t need to learn so many library-specific rules or conventions. If you’re already familiar with Scala’s standard library APIs and basic SQLs, that much should be enough.

val name = "Alice"
// implicit session represents java.sql.Connection
val memberId: Option[Long] = DB readOnly { implicit session =>
  sql"select id from members where name = ${name}" // don't worry, prevents SQL injection
    .map(rs => rs.long("id")) // extracts values from rich java.sql.ResultSet
    .single                   // single, list, traversable
    .apply()                  // Side effect!!! runs the SQL using Connection
}

See in detail: /documentation/operations

ScalikeJDBC ORM

If you’re looking for a richer database solution, scalikejdbc-orm, which is available since version 4.3, could be a greater option for you! Refer to /documentation/orm for more details.


We’re using ScalikeJDBC!


ScalikeJDBC has become the trusted go-to for countless companies in their software endeavors! Join the thriving community of developers leveraging ScalikeJDBC’s power and reliability to streamline database access and enhance project efficiency.


Metacommerce


eventsofa



ad-dice Co., Ltd.



Maverick., Inc.




Zencom Ltd.

M3, Inc.
Nulab Inc.


OPT, Inc.



Tadu.vn

Tech to Value

Read more..


If this webpage has a typo or something wrong, Please report or fix it. How?