Connecting to Splice Machine with Java and JDBC

This topic shows you how to compile and run a sample Java program that connects to Splice Machine using our JDBC driver. The SampleJDBC program 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

Follow the written directions below, which includes the raw code for the SampleJDBC example program.

Compile and Run the Sample Program

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

  1. Locate the Splice Machine JDBC Driver:

    Our JDBC driver is automatically installed on your computer(s) when you install Splice Machine. You’ll find it in the jdbc-driver folder under the splicemachine directory; typically:

    /splicemachine/jdbc-driver/
    

    - or -

    /splicemachine/jdbc-driver/db-client-2.5.0.1734.jar
    

    The build number, e.g. 1729, varies with each Splice Machine software update.

  2. Copy the example program code:

    You can copy and paste the code below:

    package com.splicemachine.cs.tools
    
    import java.sql.*;
    
    /**
    * Simple example that establishes a connection with splice and does a few basic JDBC operations
    */
    public class SampleJDBC {
    public static void main(String[] arg) {
        //JDBC Connection String - sample connects to local database
        String dbUrl = "jdbc:splice://localhost:1527/splicedb;user=splice;password=admin";
        try{
            //For the JDBC Driver - Use the Apache Derby Client Driver
            Class.forName("com.splicemachine.db.jdbc.ClientDriver");
        }catch(ClassNotFoundException cne){
            cne.printStackTrace();
            return; //exit early if we can't find the driver
        }
    
        try(Connection conn = DriverManager.getConnection(dbUrl)){
            //Create a statement
            try(Statement statement = conn.createStatement()){
    
                //Create a table
                statement.execute("CREATE TABLE MYTESTTABLE(a int, b varchar(30))");
    
                //Insert data into the table
                statement.execute("insert into MYTESTTABLE values (1,'a')");
                statement.execute("insert into MYTESTTABLE values (2,'b')");
                statement.execute("insert into MYTESTTABLE values (3,'c')");
                statement.execute("insert into MYTESTTABLE values (4,'c')");
                statement.execute("insert into MYTESTTABLE values (5,'c')");
    
                int counter=0;
                //Execute a Query
                try(ResultSet rs=statement.executeQuery("select a, b from MYTESTTABLE")){
                    while(rs.next()){
                        counter++;
                        int val_a=rs.getInt(1);
                        String val_b=rs.getString(2);
                        System.out.println("record=["+counter+"] a=["+val_a+"] b=["+val_b+"]");
                    }
                }
            }
    
        }catch (SQLException se) {
            se.printStackTrace();
        }
    }
    

    Note that the code uses the default JDBC URL and driver class values:

    Connection Parameter Default Value Comments
    JDBC URL jdbc:splice://<hostname>:1527/splicedb

    Use localhost as the <hostname> value for the standalone version of Splice Machine.

    On a cluster, specify the IP address of an HBase RegionServer.

    JDBC driver class com.splicemachine.db.jdbc.ClientDriver  
  3. Compile the code

    Compile and package the code into splice-jdbc-test-0.1.0-SNAPSHOT.jar.

  4. Run the program:

    When you run the program, your CLASSPATH must include the path to the Splice Machine JDBC driver. If you did compile and package your code into splice-jdbc-test-0.1.0-SNAPSHOT.jar, you can run the program with this command line:

    java -cp splice-installer-<platformVersion>/resources/jdbc-driver/ com.splicemachine.cs.tools.SampleJDBC
    

    The command should display a result like the following:

    record=[1] a=[1] b=[a]
    record=[2] a=[2] b=[b]
    record=[3] a=[3] b=[c]
    record=[4] a=[4] b=[c]
    record=[5] a=[5] b=[c]