JDBC

MapD Core Database supports JDBC connections.

You can find the JAR file at $MAPD_PATH/bin/mapdjdbc-1.0-SNAPSHOT-jar-with-dependencies.jar

You can find example code in $MAPD_PATH/samples:

The driver is com.mapd.jdbc.MapDDriver

The URL is jdbc:mapd:<machine>:<port>:<dbname>

  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;

  public class Flights {

  // JDBC driver name and database URL
  static final String JDBC_DRIVER = "com.mapd.jdbc.MapDDriver";
  static final String DB_URL = "jdbc:mapd:localhost:9091:mapd";

  //  Database credentials
  static final String USER = "myUserName";
  static final String PASS = "myPassWord";

  public static void main(String[] args) {
    Connection conn = null;
Statement stmt = null;
    try {
      //STEP 1: Register the JDBC driver
      Class.forName(JDBC_DRIVER);

      //STEP 2: Open a connection
      conn = DriverManager.getConnection(DB_URL, USER, PASS);

      //STEP 3: Execute a query
      stmt = conn.createStatement();
      String sql = “CREATE table flights(arr_timestamp timestamp, dep_timestamp timestamp, uniquecarrier varchar(50))”;
      stmt.executeUpdate(sql);
      sql = “insert into flights values(‘2017-04-23 06:30:0’, ‘2017-04-23 07:45:00’, ‘Southwest’)”;
      stmt.executeUpdate(sql);
      sql = “insert into flights values(‘2017-04-23 06:50:0’, ‘2017-04-23 09:45:00’, ‘American’)”;
      stmt.executeUpdate(sql);
      sql = “insert into flights values(‘2017-04-23 09:30:0’,’ 2017-04-23 12:45:00’, ‘United’)”;
      stmt.executeUpdate(sql);

      sql = "SELECT uniquecarrier from flights";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 4: Extract data from the result set
      while (rs.next()) {
        String uniquecarrier = rs.getString("uniquecarrier");
        System.out.println("uniquecarrier: " + uniquecarrier);
      }

      //STEP 5: Clean up the environment
      rs.close();
      stmt.close();
      conn.close();
    } catch (SQLException se) {
      //Handle errors for JDBC
      se.printStackTrace();
    } catch (Exception e) {
      //Handle errors for Class.forName
      e.printStackTrace();
      if (stmt != null)
          stmt.close();
      if (conn != null)
          conn.close();
     }//end try
    }//end main
  }//end Flights

To compile and execute this example:

javac Flights.java
java -cp $MAPD_PATH/bin/mapdjdbc-1.0-SNAPSHOT-jar-with-dependencies.jar:./  Flights

Supported JDBC Methods

MapD Core supports the JDBC methods listed below.

Method Description
Connection class
clearWarnings Clears all warnings reported for this Connection object. After a call to this method, the method getWarnings returns null until a new warning is reported for this Connection object.
close Disconnects the JDBC client session and frees all associated resources.
createStatement Returns a new, empty Statement.
getCatalog Retrieves this Connection object’s current catalog name.
getMetaData Retrieves a DatabaseMetaData object that contains metadata about the database to which this Connection object represents a connection. The metadata includes information about the database’s tables, its supported SQL grammar, its stored procedures, the capabilities of this Connection, and so on.
getWarnings Retrieves the first warning reported by calls on this Connection object.
isClosed Retrieves whether this Connection object has been closed.
isValid Returns true if the connection has not been closed and is still valid.
prepareStatement Creates a PreparedStatement object. Note: MapD Core makes no distinction between prepared and directly executed statements and queries.
Driver class
acceptsURL Retrieves whether the driver thinks that it can open a connection to the given URL.
connect Attempts to make a database connection to the given URL.
getMajorVersion Gets the driver’s major version number.
getMinorVersion Gets the driver’s minor version number.
getParentLogger Returns the parent Logger of all Loggers used by this driver.
getPropertyInfo Gets information about the possible properties for this driver.
jdbcCompliant Reports whether this driver is a genuine JDBC-compliant driver.
Statement
executeQuery Executes a SELECT query.
executeUpdate Executes an INSERT or DROP statement.
getMaxFieldSize Retrieves the maximum number of bytes that can be returned for character and binary column values in a ResultSet object produced by this Statement object.
getMaxRows Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain.
getMetaData Returns a new DatabaseMetaData object.
setMaxFieldSize Sets the limit for the maximum number of bytes that can be returned for character and binary column values in a ResultSet object produced by this Statement object.
setMaxRows Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number. Default is 100,000 rows.
PreparedStatement
addBatch Adds an INSERT statement to a batch.
execute Executes a ‘prepared’ query. Note: MapD Core makes no distinction between prepared and direct query execution.
executeBatch Executes a batch of queries.
executeUpdate Creates a prepared statement object for batch updates.
set[obj] Sets a dynamic parameter for batch statements. NOTE: dynamic parameters are supported with batch inserts only.
setMaxRows Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number. Default is 100,000 rows.

You can connect to a JDBC session using the HTTP protocol by appending the http option to a URL. jdbc:mapd:localhost:9092:mapd:http

Unsupported Features

MapD does not support the following JDBC features at this time.

  • Transaction statements
  • Cursors
  • Table updates, alterations or deletions
  • Multiple result sets
  • Domains
  • Rules
  • Database procedures
  • Indexes
  • Query cancellation
  • Keys
  • Constraints
  • Permissions
  • Schemas (table ownership) - any client can see tables and views created by any user without restriction