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.
Join the DZone community and get the full member experience.
Join For FreeThis 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
- 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(); } }
- Compile it. Compile and package the code into
splice-jdbc-test-<version>-SNAPSHOT.jar
. - 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 intosplice-jdbc-test-<version>-SNAPSHOT.jar
you can run the program with this command line:
The command should display a result like the following:java -cp splice-installer-<platformVersion>/resources/jdbc-driver/splicedriver-<version>-SNAPSHOT.jar com.splicemachine.cs.tools.SampleJDBC
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!
Published at DZone with permission of Gary Hillerson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments