Connecting to Splice Machine with Scala and JDBC

This topic shows you how to compile and run a sample Scala program that connects to Splice Machine using our JDBC driver. The SampleScalaJDBC program does the following:

  • connects to a standalone (localhost) version of Splice Machine
  • creates a table named MYTESTTABLE
  • inserts several sample records
  • selects and displays records from one of the system tables

Compile and Run the Sample Program

This section walks you through compiling and running the SampleScalaJDBC example program, in the following steps:

  1. Add the Splice client jar to your CLASSPATH; for example:

    export CLASSPATH=/splicemachine/jdbc-driver/splicemachine/jdbc-driver/
    
  2. Copy the example program code:

    You can copy and paste the code below; note that this example uses our default connectivity parameters (database, user, URL, and password values):

    package com.splicemachine.tutorials.jdbc
    
    import java.sql.DriverManager
    import 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=YourUserId;password=YourPassword"
    
    		var connection:Connection = null
    
    		try {
    
    			// make the connection
    			Class.forName(driver)
    			connection = DriverManager.getConnection(dbUrl)
    
    			// create the statement
    			var statement = connection.createStatement()
    
    			//Create a table
    			statement.execute("CREATE TABLE MYTESTTABLE(a int, b varchar(30))");
    			statement.close
    
    			//Insert data into the table
    			var pst = connection.prepareStatement("insert into MYTESTTABLE (a,b)  values (?,?)")
    
    			pst.setInt (1, 1)
    			pst.setString (2, "a")
    			pst.executeUpdate()
    
    			pst.clearParameters()
    			pst.setInt (1, 2)
    			pst.setString (2, "b")
    			pst.executeUpdate()
    
    			pst.clearParameters()
    			pst.setInt (1, 3)
    			pst.setString (2, "c")
    			pst.executeUpdate()
    
    			pst.clearParameters()
    			pst.setInt (1, 4)
    			pst.setString (2, "c")
    			pst.executeUpdate()
    
    			pst.clearParameters()
    			pst.setInt (1, 5)
    			pst.setString (2, "c")
    			pst.executeUpdate()
    
    			pst.close
    
    			//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 + "]")
    			}
    			resultSet.close()
    			statement.close()
    		} catch {
    			case ex : java.sql.SQLException => println("SQLException: "+ex)
    		} finally {
    			connection.close()
    		}
    	}
    
    }
    
  3. Save the code to SampleScalaJDBC.scala.

  4. Compile the program:

    scalac SampleScalaJDBC.scala
    
  5. Run the program:

    Run the SampleScalaJDBC program as follows:

    scala SampleScalaJDBC
    

    The command should display a result like the following:

    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]