DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Connecting Java to Splice Machine DB via JDBC

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.

Gary Hillerson user avatar by
Gary Hillerson
·
Apr. 29, 17 · Database Zone · Tutorial
Like (1)
Save
Tweet
2.75K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Splice (platform) Machine Java (programming language) Java Database Connectivity

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Understand Source Code — Deep Into the Codebase, Locally and in Production
  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • C++ Creator Bjarne Stroustrup Interview
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo