JBoss.orgCommunity Documentation

Chapter 6. Connecting to a VDB through JDBC

6.1. Stand-alone Java Application Deployment
6.2. Testing Your Teiid Deployment

At this point you have deployed Teiid and your VDB. Now it's time to connect the sample application to this VDB, issue SQL queries, and view the returned, integrated data. Note that this process is no different than connecting to any other JDBC source.

For a Java application to connect to a JDBC source, it needs a URL, user-id, and password. To connect to your VDB all you need is a URL and any additional optional properties that you would like to set. Teiid defaults to allowing the admin user with password teiid. Additional user accounts can be added through Teiid extensible MembershipDomain mechanism. A JDBC connection can be obtained through the Teiid driver "org.teiid.jdbc.TeiidDriver" with the URL syntax of

jdbc:teiid:<VDB-Name>@((mm|mms)://<host name>:<port>)|(<path to deploy.properties>)

When the path to deploy.properties is specified Teiid operates in embedded mode and runs in the same VM as the application.

You can add optional properties at the end of the URL using a semi-colon(;) name=value format. For example

jdbc:metamatrix:<VDB-Name>;dqp.logFile=/home/query.log

You can use any of these optional connection properties in your URL. Here is sample code showing how to make JDBC connection.

      public void execute() throws SQLException {
          String url = "jdbc:metamatrix:Portfolio@mm://localhost:31000";
          String sql = "select firstname, lastname from customer";
          
          Class.forName("org.teiid.jdbc.TeiidDriver");
          
          Connection connection;
          try{
              connection = DriverManager.getConnection(url, "admin", "teiid");
              Statement statement = connection.createStatement();
              ResultSet results = statement.executeQuery(sql);
              while(results.next()) {
                System.out.println(results.getString(1));
                System.out.println(results.getString(2));
                ...
              }
              results.close();
              statement.close();
          } catch (SQLException e){
              e.printStackTrace();
              throw e;
          } finally {
              try{
                connection.close();
              }catch(SQLException e1){
                // ignore
              }              
          }
      }      
    

You can also use org.teiid.jdbc.TeiidDataSource to make connection in your Java application. For example, you can use following code fragment to make a connection to the VDB and issuing the query exactly same as in the above example

        TeiidDataSource ds = new TeiidDataSource(); 
        ds.setDatabaseName("Portfolio");
        ds.setEmbeddedBootstrapFile("<teiid home>/deploy.properties");
        ds.setUser("admin");
        ds.setPassword("teiid");
        
        Connection connection = ds.getConnection();
        ...
      

TeiidDataSource source also provides an option to set optional parameters using the "set" methods on the data source look. For all the allowable properties at the data source properties.

The Teiid installation includes a simple Java class which demonstrates JDBC access of the deployed VDB. To execute this demonstration, follow these steps:

The sample query, "select * from CustomerAccount", queries the view model and, from that, queries the two underlying data sources: one relational, one file-based. All the sample Java class does is connect to the VDB, issue the query, and print the results. Teiid does the "heavy lifting" of integrating the results. (For the complete code used in this example, look in the "examples/portfolio" directory.)

You are encouraged to experiment with queries that go beyond the simple "select * from CustomerAccount".

If your application is Web based, you can also deploy Teiid in an application server and treat it as any other JDBC source by creating a connection pool with the org.teiid.jdbc.TeiidDataSource and assigning it a JNDI name. Refer to deployment to application server for more information.