teiid-{version}-jdbc.jar
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 sources like Oracle.
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 jar file is available for download from here Quick Start Example#Download
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.
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.
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.
The Teiid installation includes a simple Java class which demonstrates JDBC access to the deployed VDB. To execute this demonstration, follow these steps:
Change to the "<jboss-install>/docs/teiid/examples/simpleclient" directory
Execute the "run" script (either for Linux or Windows)
Depending on the VDB you deployed, see the relevant README file for example queries.
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 using the above 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 deployment for more information on creating a DataSource.
Congratulations! that is end of this tutorial. Ask us any questions you have on Teiid forums