GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.voltdb.com — 15.3. JDBC Interface Documentation15.3. JDBC InterfaceJDBC (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. 15.3.1. Using JDBC to Connect to a VoltDB DatabaseThe 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");15.3.2. Using JDBC to Query a VoltDB DatabaseOnce the connection is made, you use the standard JDBC classes and
methods to access the database. (See the JDBC documentation at The following is a complete example that uses JDBC to access the
Hello World tutorial that comes with the VoltDB software in the
subdirectory 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();
}
}
} |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |