Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Connecting Java to Splice Machine DB via JDBC

DZone's Guide to

Connecting Java to Splice Machine DB via JDBC

Use the SampleJDBC program to programmatically connect with your Splice Machine database using Java and our JDBC driver.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

This topic explains how to programmatically connect with your Splice Machine database using Java and our JDBC driver.

Using the Splice Machine JDBC Driver

To use our JDBC driver with Java, you need to locate the Splice Machine client JAR file, which is automatically installed for you when you install Splice Machine. Follow the instructions in our Using the Splice Machine JDBC Driver topic to locate the JDBC driver and to review its configuration information.

Example Program

This section contains the SampleJDBC 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.

SampleJDBC

  1. Copy the code.
    
    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();
    	}
    }
  2. Compile it. Compile and package the code into splice-jdbc-test-<version>-SNAPSHOT.jar.
  3. Run the program. When you run the program, your CLASSPATH must include the path to the splice JDBC driver. If you did compile and package your code into splice-jdbc-test-<version>-SNAPSHOT.jar you can run the program with this command line:
    java -cp splice-installer-<platformVersion>/resources/jdbc-driver/splicedriver-<version>-SNAPSHOT.jar com.splicemachine.cs.tools.SampleJDBC   
    The command should display a result like the following:
    java -cp ./target/splice-cs--<version>-SNAPSHOT.jar com.splicemachine.cs.tools.SampleJDBC
    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]

And that's it!

Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

Topics:
splice machine ,database ,jdbc ,tutorial

Published at DZone with permission of Gary Hillerson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}