JBoss Community Archive (Read Only)

Teiid Examples

Connecting to Teiid - SimpleClient Example

The simple client example will assist in demonstrating how to make a connection to Teiid using either the JDBC Driver or a DataSource.    Note, this process is no different than connecting to any other JDBC sources like Oracle.

simpleclient quickstart
All the code discussed in this tutorial is available in the simple-client quickstart .

See the README.md for directions to run the quick start.

Sample Client

The sample client example can be used to test your sql queries against any server running a compatible Teiid Runtime and has an active VDB.    This example will also be referenced by many of the other quick starts as a means to test suggested queries.  Be sure to read the README.md for that quick start in order to setup the environment correctly before executing a test query.  

If you are using a graphical client, such as SQuirreL, have a look at the metadata tree to see not only what is exposed by your VDB, but also the SYS schema tables.

If your application is Web based, you can create a data source for your VDB and treat it as any other JDBC source using org.teiid.jdbc.TeiidDataSource and assigning it a JNDI name. Refer to Client Developer's Guide - JBoss AS Datasource deployment for more information on creating a DataSource.

Standalone Java Application

Before you can make a JDBC connection to the Teiid VDB, add the Teiid's JDBC driver jar file to your application's classpath. This JDBC Driver jar file is available for download from  Teiid Downloads .

teiid-{version}-jdbc.jar

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

jdbc:teiid:<VDB-Name>@mm(s)://<host name>:<port>

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

jdbc:teiid:<VDB-Name>@mm(s)://<host name>:<port>;user=user;password=user

Check out  Client Developer's Guide  for all the optional connection properties in your URL. Here is sample code showing how to make JDBC connection.

Sample JAVA Code
public void execute() throws SQLException {
    String url = "jdbc:teiid:DynamicPortfolio@mm://localhost:31000";
    String sql = "select firstname, lastname from customer";

    Class.forName("org.teiid.jdbc.TeiidDriver");

    Connection connection;
    try{
        connection = DriverManager.getConnection(url, "user", "user");
        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 the same as in the above example:

TeiidDataSource ds = new TeiidDataSource();
ds.setDatabaseName("DynamicPortfolio");
ds.setUser("user");
ds.setPassword("user");

Connection connection = ds.getConnection();
...

The TeiidDataSource also provides an option to set optional parameters using the "set" methods on the data source. For all the allowable data source properties, check out  Client Developer's Guide .

Remote Server

If your Teiid client and server are not installed on same machine, then you have to bind JBoss AS IP address on to something other than 127.0.0.1 Contact your system administrator for the IP address you can use for that machine so that it can be remotely accessible. Once you have IP address or hostname you can update the standalone-teiid.xml file under "interfaces" section. Once you restart the server, then you can specify the correct IP address or hostname in the Teiid client driver to make the remote connection to the VDB.

If you have any questions, please ask them on the Teiid forums

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-13 13:14:06 UTC, last content change 2012-11-14 19:07:48 UTC.