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

RavenDB vs MongoDB: Which is Better? This White Paper compares the two leading NoSQL Document Databases on 9 features to find out which is the best solution for your next project.  

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!

Get comfortable using NoSQL in a free, self-directed learning course provided by RavenDB. Learn to create fully-functional real-world programs on NoSQL Databases. Register today.

Topics:
splice machine ,database ,jdbc ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}