Skip to content

Commit eb55dd4

Browse files
Add more API examples of batch queries, job status and query state (#23)
Signed-off-by: Sanjula Ganepola <Sanjula.Ganepola@ibm.com>
1 parent 712fd8e commit eb55dd4

File tree

1 file changed

+81
-2
lines changed
  • src/content/docs/guides/Usage

1 file changed

+81
-2
lines changed

src/content/docs/guides/Usage/java.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ Using DB2 for IBM i with Java is now easy with the help of the `mapepire-java` c
2323

2424
## Specifying the `mapepire-server` Instance for the Connection
2525

26-
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`](https://github.com/Mapepire-IBMi/samples/blob/main/java/simple-app/src/main/resources/config.properties.sample) file from the [simple-app](https://github.com/Mapepire-IBMi/samples/tree/main/java/simple-app) demo project to `config.properties` and fill in the credentials.
26+
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`](https://github.com/Mapepire-IBMi/samples/blob/main/java/simple-app/src/main/resources/config.properties.sample) file from the [simple-app](https://github.com/Mapepire-IBMi/samples/tree/main/java/simple-app) demo project to `config.properties` and fill in the credentials. Note that the `IBMI_PORT` is set to where the mapepire server is running.
27+
28+
```ini
29+
IBMI_HOST=host.somewhere.com
30+
IBMI_USER=JIMBOB
31+
IBMI_PASSWORD=letMeInNow
32+
IBMI_PORT=8076
33+
```
2734

2835
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.
2936

@@ -64,11 +71,52 @@ job.connect(creds).get();
6471

6572
// Initialize and execute query
6673
Query query = job.query("SELECT * FROM SAMPLE.DEPARTMENT");
67-
QueryResult<Object> result = query.execute().get();
74+
QueryResult<Object> result = query.execute(3).get();
6875

6976
// Close query and job
7077
query.close().get();
7178
job.close();
79+
80+
// Convert to JSON string and output
81+
ObjectMapper mapper = new ObjectMapper();
82+
mapper.enable(SerializationFeature.INDENT_OUTPUT);
83+
String jsonString = mapper.writeValueAsString(result);
84+
System.out.println(jsonString);
85+
```
86+
87+
Output:
88+
89+
```json
90+
{
91+
"id" : "query3",
92+
"success" : true,
93+
"error" : null,
94+
"sql_rc" : 0,
95+
"sql_state" : null,
96+
"execution_time" : 174,
97+
"metadata" : {
98+
"column_count" : 5,
99+
"columns" : [
100+
{ "display_size" : 3, "label" : "DEPTNO", "name" : "DEPTNO", "type" : "CHAR", "precision" : 3, "scale" : 0 },
101+
{ "display_size" : 36, "label" : "DEPTNAME", "name" : "DEPTNAME", "type" : "VARCHAR", "precision" : 36, "scale" : 0 },
102+
{ "display_size" : 6, "label" : "MGRNO", "name" : "MGRNO", "type" : "CHAR", "precision" : 6, "scale" : 0 },
103+
{ "display_size" : 3, "label" : "ADMRDEPT", "name" : "ADMRDEPT", "type" : "CHAR", "precision" : 3, "scale" : 0 },
104+
{ "display_size" : 16, "label" : "LOCATION", "name" : "LOCATION", "type" : "CHAR", "precision" : 16, "scale" : 0 }
105+
],
106+
"job" : "058971/QUSER/QZDASOINIT",
107+
"parameters" : null
108+
},
109+
"is_done" : false,
110+
"has_results" : true,
111+
"update_count" : -1,
112+
"data" : [
113+
{ "DEPTNO" : "A00", "DEPTNAME" : "SPIFFY COMPUTER SERVICE DIV.", "MGRNO" : "000010", "ADMRDEPT" : "A00", "LOCATION" : null },
114+
{ "DEPTNO" : "B01", "DEPTNAME" : "PLANNING", "MGRNO" : "000020", "ADMRDEPT" : "A00", "LOCATION" : null },
115+
{ "DEPTNO" : "C01", "DEPTNAME" : "INFORMATION CENTER", "MGRNO" : "000030", "ADMRDEPT" : "A00", "LOCATION" : null }
116+
],
117+
"parameter_count" : 0,
118+
"output_parms" : null
119+
}
72120
```
73121

74122
### Prepared Statements
@@ -81,6 +129,21 @@ Query query = job.query("SELECT * FROM SAMPLE.DEPARTMENT WHERE ADMRDEPT = ?", op
81129
QueryResult<Object> result = query.execute().get();
82130
```
83131

132+
### Batch Queries
133+
134+
Multiple queries can be executed in a batch:
135+
136+
```java
137+
QueryOptions options = new QueryOptions(false, false, Arrays.asList(
138+
Arrays.asList("SAM", "416 345 0879"),
139+
Arrays.asList("BOB", "647 821 7261"),
140+
Arrays.asList("JOHN", "289 726 1823"),
141+
Arrays.asList("JANE", "416 345 0879")
142+
));
143+
Query query = job.query("INSERT INTO SAMPLE.EMPLOYEE VALUES (?, ?)", options);
144+
QueryResult<Object> result = query.execute().get();
145+
```
146+
84147
### CL Commands
85148

86149
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:
@@ -145,6 +208,22 @@ PoolOptions poolOptions = new PoolOptions(creds, jdbcOptions, 5, 3);
145208
Pool pool = new Pool(poolOptions);
146209
```
147210

211+
## Job Status and Query State
212+
213+
For any `SqlJob` object, you can check its status via its [JobStatus](https://github.com/Mapepire-IBMi/mapepire-java/blob/main/src/main/java/io/github/mapepire_ibmi/types/JobStatus.java):
214+
215+
```java
216+
SqlJob job = new SqlJob();
217+
JobStatus status = job.getStatus();
218+
```
219+
220+
Similarily, you can check the state of a query via its [QueryState](https://github.com/Mapepire-IBMi/mapepire-java/blob/main/src/main/java/io/github/mapepire_ibmi/types/QueryState.java):
221+
222+
```java
223+
Query query = job.query("SELECT * FROM SAMPLE.DEPARTMENT");
224+
QueryState state = query.getState();
225+
```
226+
148227
## Exception Handling
149228

150229
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:

0 commit comments

Comments
 (0)