JDBC (Java Database Connectivity) is a programming interface for Java programmers that abstracts database specifics from the methods used to access the data. JDBC provides standard methods and classes for accessing a relational database and vendors then provide JDBC drivers to implement the abstracted methods on their specific software.
VoltDB provides a JDBC driver for those who would prefer to use JDBC as the data access interface. The VoltDB JDBC driver supports ad hoc queries, prepared statements, calling stored procedures, and methods for examining the metadata that describes the database schema.
The VoltDB driver is a standard class within the VoltDB software jar. To load the driver you use the Class.forName method to load the class org.voltdb.jdbc.Driver.
Once the driver is loaded, you create a connection to a running VoltDB database server by constructing a JDBC url using the "jdbc:" protocol, followed by "voltdb://", the server name, a colon, and the port number. In other words, the complete JDBC connection url is "jdbc:voltdb://{server}:{port}". To connect to multiple nodes in the cluster, use a comma separated list of server names and port numbers after the "jdbc:voltdb://" prefix.
For example, the following code loads the VoltDB JDBC driver and connects to the servers svr1 and svr2 using the default client port:
Class.forName("org.voltdb.jdbc.Driver"); Connection c = DriverManager.getConnection( "jdbc:voltdb://svr1:21212,svr2:21212");
Once the connection is made, you use the standard JDBC classes and
methods to access the database. (See the JDBC documentation at http://download.oracle.com/javase/6/docs/technotes/guides/jdbc
for details.) Note, however, when running the JDBC application, you must
make sure both the VoltDB software jar and the Guava library are in the
Java classpath. Guava is a third party library that is shipped as part of
the VoltDB kit in the /lib directory. Unless you include both components
in the classpath, your application will not be able to find and load the
necessary driver class.
The following is a complete example that uses JDBC to access the
Hello World tutorial that comes with the VoltDB software in the
subdirectory /doc/tutorials/helloworld
. The JDBC demo
program executes both an ad hoc query and a call to the VoltDB stored
procedure, Select.
import java.sql.*; import java.io.*; public class JdbcDemo { public static void main(String[] args) { String driver = "org.voltdb.jdbc.Driver"; String url = "jdbc:voltdb://localhost:21212"; String sql = "SELECT dialect FROM helloworld"; try { // Load driver. Create connection. Class.forName(driver); Connection conn = DriverManager.getConnection(url); // create a statement Statement query = conn.createStatement(); ResultSet results = query.executeQuery(sql); while (results.next()) { System.out.println("Language is " + results.getString(1)); } // call a stored procedure CallableStatement proc = conn.prepareCall("{call Select(?)}"); proc.setString(1, "French"); results = proc.executeQuery(); while (results.next()) { System.out.printf("%s, %s!\n", results.getString(1), results.getString(2)); } //Close statements, connections, etc. query.close(); proc.close(); results.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }