Over a million developers have joined DZone.

Connecting Scala to Splice Machine

DZone's Guide to

Connecting Scala to Splice Machine

Scala is increasingly being used in the field of Big Data analytics. Learn how to use JDBC in Scala to connect to Splice Machine.

· Database Zone ·
Free Resource

Find out how Database DevOps helps your team deliver value quicker while keeping your data safe and your organization compliant. Align DevOps for your applications with DevOps for your SQL Server databases to discover the advantages of true Database DevOps, brought to you in partnership with Redgate

Scala is a concise, expressive, and powerful programming language that is increasingly becoming the language of choice in the field of Big Data analytics. Apache Spark™ is a prime example of a fast data analytics platform that was built from the ground-up using Scala. Real-world applications that access big data systems and traditional relational database management systems (RDBMS) need easy connectivity to both Spark and Scala.

Scala code gets compiled into bytecode that runs on a Java™ virtual machine (JVM), which allows Scala applications to directly call Java libraries. Which means that it’s a simple matter of using the Splice Machine JDBC driver to connect to your Splice Machine database from a Scala application.

To use JDBC with Splice Machine, you need to install the Splice Machine JDBC driver, as described in the JDBC Driver topic in the Splice Machine documentation.

To download and learn about Scala, visit the Scala language website.

Example Program

This section contains the SampleScalaJDBC program, which does the following:

  • Connects to a standalone (localhost) version of Splice Machine.
  • Creates a table named MYTESTTABLE.
  • Inserts several sample records.
  • Issues a query to retrieve those records.


To run the SampleScalaJDBC program, follow these steps:

  1. Copy the code that’s shown below and can be downloaded from GIST.
  2. Compile and package the code into splice-jdbc-test-0.0.1-SNAPSHOT.jar.
  3. Make sure your CLASSPATH includes the path to the Splice Machine JDBC driver.
  4. Run the program with this command:
scala -cp <path to jdbc driver jar>/splicedriver-  com.splicemachine.tutorials.jdbc.SampleScalaJDBC

Your results should look like this:

record=[1] a=[5] b=[c]

record=[2] a=[1] b=[a]

record=[3] a=[2] b=[b]

record=[4] a=[3] b=[c]

record=[5] a=[4] b=[c]

We're about to get to the code, but first, please note that you should replace the following table with this GIST: 

<script src=”https://gist.github.com/ghillerson/8fe8a52bc23d1d947ef40cad664b5272.js”></script>

Now for the code:

package com.splicemachine.tutorials.jdbc
import java.sql.DriverManagerimport java.sql.Connection
 /** *  Simple example of Establishes a connection with splice and executes statements */
 object SampleScalaJDBC {
  def main(args: Array[String]) {
   // connect to the database named “splicedb” on the localhost
   val driver = “com.splicemachine.db.jdbc.ClientDriver”

   val dbUrl = “jdbc: splice: //localhost:1527/splicedb;user=splice;password=admin”
    var connection: Connection = null

   try {
    // make the connection


    connection = DriverManager.getConnection(dbUrl)
     // create the statement

    var statement = connection.createStatement()

    //Create a table

    statement.execute(“CREATE TABLE MYTESTTABLE(a int, b varchar(30))”);

     //Insert data into the table

    var pst = connection.prepareStatement(“insert into MYTESTTABLE(a, b) values( ? , ? )”)

    pst.setInt(1, 1)

    pst.setString(2, “a”)


    pst.setInt(1, 2)

    pst.setString(2, “b”)


    pst.setInt(1, 3)

    pst.setString(2, “c”)


    pst.setInt(1, 4)

    pst.setString(2, “c”)


    pst.setInt(1, 5)

    pst.setString(2, “c”)

     //Read the data

    statement = connection.createStatement()

    val resultSet = statement.executeQuery(“select a, b from MYTESTTABLE”)

    var counter = 0
    while (resultSet.next()) {

     counter += 1

     val val_a = resultSet.getInt(1)

     val val_b = resultSet.getString(2)

     println(“record = [”+counter + “] a = [”+val_a + “] b = [”+val_b + “]”)




   } catch {

    case ex:
     java.sql.SQLException => println(“SQLException: “+ex)

   } finally {




And that's it!

Align DevOps for your applications with DevOps for your SQL Server databases to increase speed of delivery and keep data safe. Discover true Database DevOps, brought to you in partnership with Redgate

splice machine ,scala ,jdbc ,tutorial ,database

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}