A simple ScalikeJdbc SQL SELECT example

I don’t have time to write about ScalikeJdbc right now, so I’ll just say that it’s been working well for me. Here’s one of the first test examples I created, a simple SQL SELECT query that prints some output from a database table:

import scalikejdbc._
import scalikejdbc.config._

/**
 * this example depends on the database settings in the file
 * src/main/resources/application.conf
 * @see http://scalikejdbc.org/documentation/configuration.html
 * (see the “scalikejdbc-config” section)
 */
object Scalike2 extends App {

    // DBs.setup/DBs.setupAll loads specified JDBC driver classes.
    DBs.setupAll()
    
    // see http://scalikejdbc.org/documentation/operations.html
    // for query examples
    DB readOnly { implicit session =>
        sql"select first from users".foreach { rs =>
            println(rs.string("first"))
        }
    }

    // wipes out ConnectionPool
    DBs.closeAll()

}

This little application depends on an application.conf configuration file, which looks like this:

# JDBC settings
db.default.driver="org.h2.Driver"
db.default.url="jdbc:h2:./test"
db.default.user="sa"
db.default.password="sa"

# Connection Pool settings
db.default.poolInitialSize=5
db.default.poolMaxSize=10
db.default.poolConnectionTimeoutMillis=1000

At some point I hope to write more about ScalikeJdbc, but until then I’ll try to share examples that I hope will help others.