Using the Profile in JDBC
This section provides a walk-through of writing data access code to this profile in JDBC using the CData JDBC driver for API.
Connecting from Code
When connecting with the DriverManager class, the CData JDBC Driver for API 2019 follows the JDBC convention: First, load the API driver class. Then, make a connection.
Load the Driver
The following step is optional per the JDBC 4.0 specification.
Class.forName("cdata.jdbc.api.APIDriver");
Establish a Connection
Provide the connection string with the getConnection method of the static DriverManager class. Start the connection string with "jdbc:api:". A typical connection string is the following:
Connection conn = DriverManager.getConnection("jdbc:api:Profile=MyProfile.apip;ProfileSettings=[Profile Configuration Settings]");
Alternatively, you can prepare the connection options using a Properties object. Pass the Properties object to the DriverManager.
Properties prop = new Properties();
prop.setProperty("Profile", "MyProfile.apip");
prop.setProperty("ProfileSettings", "[Profile Configuration Settings]");
Connection conn = DriverManager.getConnection("jdbc:api:,");
Executing Select Statements
To execute SQL statements that return data, use the Statement class' generic execute method or the executeQuery method. To return the results of a query, call the getResultSet method of the Statement.
The following example calls the execute method and iterates over the results returned:
Statement stat = conn.createStatement();
boolean ret = stat.execute("SELECT Email, Username FROM NorthwindOData");
if (ret) {
ResultSet rs=stat.getResultSet();
while(rs.next()) {
for(int i=1;i<=rs.getMetaData().getColumnCount();i++) {
System.out.println(rs.getMetaData().getColumnName(i) +"="+rs.getString(i));
}
}
}