Java
Full API docs can be found on the client SDK project page, but the basics are summarized here.
Using DB2 for IBM i with Java is now easy with the help of the mapepire-java
client SDK. To get started, install the package with maven
. Make sure to install the latest version from Maven Central.
Requirements
- Java 8 or later
Specifying the mapepire-server
Instance for the Connection
The location and port of the mapepire-server
instance as well as the credentials for IBM i Db2 can be specified in a config.properties
file. Copy the config.properties.sample
file from the simple-app demo project to config.properties
and fill in the credentials.
The following function can be used to construct a DaemonServer
object with the credentials you just specified. This object will be passed to a SqlJob
or Pool
object.
Running Queries
The core APIs lie in the SqlJob
and Query
classes which are what give you the ability to execute queries.
Prepared Statements
Statements can be easily prepared and executed with parameters:
CL Commands
CL commands can be easily run by setting the isClCommand
option to be true
on the QueryOptions
object or by directly using the clCommand
API on a job:
Paginating Results
Paginating results can be easily achieved using the rowsToFetch
parameter when executing a query along with the fetchMore
API to retrieve more results.
Pooling
To streamline the creation and reuse of SqlJob
objects, your application should establish a connection pool on startup. This is recommended as connection pools significantly improve performance as it reduces the number of connection objects that are created.
A pool can be initialized with a given starting size and maximum size. Once initialized, the pool provides APIs to access a free job or to send a query directly to a free job.
JDBC Options
When creating an SqlJob
or Pool
object, JDBC options can be specified. For a full list of all options, check out the documentation here.
Exception Handling
The APIs provided by the client SDK can throw various checked exceptions which should be caught and handled. In particular, the following exceptions communicate important error information from either the mapepire-server
component or the client SDK itself:
SQLException
- This is thrown when an SQL related exception occurs. This will also typically communicate areason
andSQLState
.UnknownServerException
- This is throw when the server hits an unknown exception.ClientException
- This is thrown when the client SDK wants to communicate an error with calling an API.
Secure Connections
By default, Mapepire will always try to connect securely. A majority of the time, servers are using their own self-signed certificate that is not signed by a recognized CA (Certificate Authority). There are two options with the Java client.
Allow All Certificates
On the DaemonServer
object, the ignoreUnauthorized
option can be set to true
which will allow either self-signed certificates or certificates from a CA.