Skip to content

Commit 08b6366

Browse files
authored
Merge pull request #15 from vlkong/master
Studio integration sample
2 parents 665d884 + dfe385f commit 08b6366

File tree

27 files changed

+380
-845
lines changed

27 files changed

+380
-845
lines changed

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ build/
77
external_libs/
88
/target/
99

10-
# eclipse files
11-
.classpath
12-
.project
13-
.settings/
10+
# I don't want the eclipse file for the root project to be in the repository,
11+
# but some examples might have eclipse projects in them
12+
/.classpath
13+
/.project
14+
/.settings/

README.DB2.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
## Build the sample
44

5-
Before you build the sample, you must edit `build.properties` for the appropriate path locations:
5+
Sample [studio_integration](examples/studio_integration) do not need to be compiled, so you can skip this
6+
section.
7+
8+
Before you build the [oil](examples/oil) sample, you must edit `build.properties` for the appropriate path locations:
69

7-
* If you want to run the sample with MySQL, `mysql.jdbc.connector.path` should point to your JDBC driver location.
810
* If you want to run the sample with IBM DB2, `db2.jdbc.connector.path` should point to your JDBC driver location.
911
* `opl.home` should point to your OPL home, unless you have a `CPLEX_STUDIO_DIR128` set. (this variable should exists if you installed on a Windows machine).
1012

@@ -38,19 +40,34 @@ Run the following SQL script to create and populate the example database:
3840
db2 -tvmf data/oil_db2.sql
3941
```
4042

41-
Before you run the sample, you need to edit `build.properties` to make `db2.jdbc.connector.path` point
42-
to your DB2 jdbc driver.
4343

4444
You can download the DB2 jdbc driver [here](http://www-01.ibm.com/support/docview.wss?uid=swg21363866).
4545
Note that if you installed DB2 Express-C, your JDBC driver is `db2jcc4.jar`
4646
in `<DB2 installdir>/SQLLIB/java`.
4747

48-
Edit `data\db_db2.xml` for your JDBC connection string and credentials.
48+
Depending on the sample you run, you need to edit, `build.properties` to make `db2.jdbc.connector.path` point
49+
to your DB2 jdbc driver.
50+
51+
Depending on the sample you run, you need to edit `build.properties` to make
52+
`db2.jdbc.connector.path` point to your DB2 jdbc driver (i.e. db2jcc4.jar).
53+
54+
In sample [studio_integration](examples/studio_integration), you will need to edit `jdbc.js` to point
55+
to your jdbc driver, *or* add an `OPL_JDBC_DRIVER` environment variable pointing to it:
56+
57+
```
58+
var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER");
59+
if (! jdbc_driver ) {
60+
jdbc_driver = "../../external_libs/db2jcc4.jar"; // default for this project
61+
}
62+
```
63+
64+
For sample [oil](examples/oil), edit `data\db_db2.xml` for your JDBC connection string and credentials.
4965
Your connection string looks like `db2://localhost:<port>/<database_name>`
5066
where `port` is the DB2 port (default is 50000), `<database_name>` is the name
5167
of your database (default is `CUSTOMDB`).
5268

53-
## Run the sample
69+
## Run the oil sample
70+
5471
Compile and run the sample for IBM DB2:
5572

5673
```
@@ -61,4 +78,12 @@ $ ant run_db2
6178
* Uses data/oil.dat as a data file
6279
* Uses data/db_db2.xml to customize the JDBC custom data source.
6380

81+
## Other samples
82+
83+
The other samples do not need to be compiled, and will use `oplrun` executable. Each sample directory
84+
contains a convenience `run.bat` command line to run the sample. Otherwise you run them using the `run` target:
85+
86+
```
87+
$ ant run
88+
```
6489

README.MySQL.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## Build the sample
44

5-
Before you build the sample, you must edit `build.properties` for the appropriate path locations:
5+
Sample [studio_integration](examples/studio_integration) do not need to be compiled, so you can skip this
6+
section.
7+
8+
Before you build the [oil](examples/oil) sample, you must edit `build.properties` for the appropriate path locations:
69

710
* If you want to run the sample with MySQL, `mysql.jdbc.connector.path` should point to your JDBC driver location.
8-
* If you want to run the sample with IBM DB2, `db2.jdbc.connector.path` should point to your JDBC driver location.
911
* `opl.home` should point to your OPL home, unless you have a `CPLEX_STUDIO_DIR128` set. (this variable should exists if you installed on a Windows machine).
1012

1113
The build file, `build.xml`, imports the build file from the OPL samples,
@@ -57,7 +59,23 @@ Once the driver is download and extracted, edit property `jdbc.connector.path` i
5759
to include the MySQL Connector/J `.jar` (should look like `mysql-connector-java-5.1.40-bin.jar`
5860
in your MySQL Connector/J extracted diretory)
5961

60-
Edit `data\db_mysql.xml` for your JDBC connection string and credentials.
62+
63+
Depending on the sample you run, you need to edit `build.properties` to make
64+
`mysql.jdbc.connector.path` point to your MySQL jdbc driver (i.e. mysql-connector-java-5.1.40-bin.jar).
65+
66+
67+
In sample [studio_integration](examples/studio_integration), you will need to edit `jdbc.js` to point
68+
to your jdbc driver, *or* add an `OPL_JDBC_DRIVER` environment variable pointing to it:
69+
70+
```
71+
var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER");
72+
if (! jdbc_driver ) {
73+
jdbc_driver = "../../external_libs/mysql-connector-java-5.1.40-bin.jar"; // default for this project
74+
}
75+
```
76+
77+
78+
For sample [oil](examples/oil), edit `data\db_mysql.xml` for your JDBC connection string and credentials.
6179
Your connection string looks like `jdbc:mysql://localhost:3306/<database_name>?useSSL=false`
6280
where `<database_name>` is the name of your database (default is `custom_data_source`).
6381

README.SQLServer.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
## Build the sample
44

5-
Before you build the sample, you must edit `build.properties` for the appropriate path locations:
5+
Sample [studio_integration](examples/studio_integration) do not need to be compiled, so you can skip this
6+
section.
67

7-
* If you want to run the sample with MySQL, `mysql.jdbc.connector.path` should point to your JDBC driver location.
8-
* If you want to run the sample with IBM DB2, `db2.jdbc.connector.path` should point to your JDBC driver location.
8+
Before you build the [oil](examples/oil) sample, you must edit `build.properties` for the appropriate path locations:
9+
10+
* `sqlserver.jdbc.connector.path` should point to your JDBC driver location.
911
* `opl.home` should point to your OPL home, unless you have a `CPLEX_STUDIO_DIR128` set. (this variable should exists if you installed on a Windows machine).
1012

1113
The build file, `build.xml`, imports the build file from the OPL samples,
@@ -20,26 +22,43 @@ The example is automatically compiled with the run Ant targets is invoked.
2022

2123
## Setup the sample database
2224

23-
To run the sample with MS SQL Server, you need to install MS SQL Server.
24-
25-
In a <em>Commnad Prompt</em> window:
25+
To run the sample with MS SQL Server, you need to install Microsoft SQL Server.
26+
After it is installed, you are ready to setup the sample database.
2627

27-
Provided your sql server instance name is SQLEXPRESS, create database using:
28+
To create a sample database, open a <em>Commnad Prompt</em> window, and Provided your
29+
SQL Server instance name is SQLEXPRESS, create database using:
2830

2931
```
3032
C:\>sqlcmd -S .\SQLEXPRESS -i data\oil_mssql.sql
3133
```
3234

33-
Before you run the sample, you need to edit `build.properties` to make `sqlserver.jdbc.connector.path` point
34-
to your MSSQL server jdbc driver (i.e. mssql-jdbc-7.2.2.jre8.jar)
35+
Before you run the sample, you need to download the [Microsoft JDBC Driver for SQL Server](https://docs.microsoft.com/fr-fr/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017).
36+
37+
Once your download finished, decompress the archive. The archive contains jar files that
38+
look like `mssql-jdbc-7.2.2.jre8.jar`.
39+
40+
41+
Depending on the sample you run, you need to edit `build.properties` to make
42+
`sqlserver.jdbc.connector.path` point to your MSSQL server jdbc driver (i.e. mssql-jdbc-7.2.2.jre8.jar).
43+
44+
In sample [studio_integration](examples/studio_integration), you will need to edit `jdbc.js` to point
45+
to your jdbc driver, *or* add an `OPL_JDBC_DRIVER` environment variable pointing to it:
46+
47+
```
48+
var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER");
49+
if (! jdbc_driver ) {
50+
jdbc_driver = "../../external_libs/mssql-jdbc-7.2.2.jre8.jar"; // default for this project
51+
}
52+
```
3553

36-
Edit `data\db_mssql.xml` for your JDBC connection string and credentials.
54+
For sample [oil](examples/oil), edit `data\db_mssql.xml` for your JDBC connection string and credentials.
3755
Your connection string looks like ` jdbc:sqlserver://localhost;instanceName=<instance>;databaseName=<database_name>;integratedSecurity=true`
3856

3957
where `instance` is the mssql instance name (default is SQLEXPRESS), `<database_name>` is the name
4058
of your database (default is `custom_data_source`).
4159

42-
## Run the sample
60+
## Run the oil sample
61+
4362
Compile and run the sample for MS SQL Server:
4463

4564
```
@@ -48,4 +67,15 @@ $ ant run_mssql
4867

4968
* Uses data/oil.mod as a model file
5069
* Uses data/oil.dat as a data file
51-
* Uses data/db_db2.xml to customize the JDBC custom data source.
70+
* Uses data/db_mssql.xml to customize the JDBC custom data source.
71+
72+
## Other samples
73+
74+
The other samples do not need to be compiled, and will use `oplrun` executable. Each sample directory
75+
contains a convenience `run.bat` command line to run the sample. Otherwise you run them using the `run` target:
76+
77+
```
78+
$ ant run
79+
```
80+
81+

README.md

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ This example will work with any 12.x OPL version, even if it is configured to ru
1616

1717
## Table of Contents
1818
- [Prerequisites](#prerequisites)
19-
- [Build and run the sample from java](#build-and-run-the-sample-from-java)
19+
- [Build and run the sample](#build-and-run-the-sample)
20+
- [Run the 'oil' sample](#run-the-oil-sample)
2021
- [Run the sample from OPL](#run-the-sample-from-opl)
21-
- [Reusing the sample with other databases](#reusing-the-sample-with-other-databases)
2222
- [Export plain dat files](#export-plain-dat-files)
2323
- [Run with another OPL version](#run-with-another-opl-version)
2424
- [License](#license)
@@ -42,77 +42,27 @@ This example will work with any 12.x OPL version, even if it is configured to ru
4242
MySQL Comunity Server, but is compatible with minimal changes with other JDBC
4343
compatible databases.
4444

45-
## Build and run the sample from java
45+
## Build and run the sample
4646

47-
The default sample uses model and data from [examples/oil](examples/oil)
47+
Before you run, you need to populate the database. See details in subsections:
4848

4949
- [Run sample with DB2](README.DB2.md)
5050
- [Run sample with MySQL](README.MySQL.md)
5151
- [Run sample with MS SQL Server](README.SQLServer.md)
5252

53+
### Run the 'oil' sample
54+
55+
The [examples/oil](examples/oil) sample demonstrates how to create a Java application with
56+
a custom data source and invoke OPL from Java.
5357

5458
### Run the sample from OPL
5559

56-
Sample in [examples/ilo_opl_call_java](examples/ilo_opl_call_java) shows how to
60+
The [examples/studio_integration](examples/studio_integration) sample shows how to
5761
use the jdbc custom data source as a library, without having the need to
5862
invoke OPL runtime from java. You can use this method to access database
5963
using a jdbc-custom-data-source from `oplrun` or OPL Studio.
6064

6165

62-
### Reusing the sample with other databases
63-
64-
As the sample is build on JDBC, it's possible to reuse <code>JdbcCustomDataSource</code> with minimal changes:
65-
66-
* Add your JDBC driver in your classpath
67-
* update db.xml with your database connection string
68-
69-
``` XML
70-
<!-- The connection string
71-
The default url connects to mysql on default port, using database
72-
'custom_data_source'
73-
-->
74-
<url>jdbc:mysql://localhost:3306/custom_data_source?useSSL=false</url>
75-
76-
<!-- Your connection credentials -->
77-
<user>sql_user</user>
78-
<password>mysql</password>
79-
```
80-
81-
* update db.xml with queries to read your data elements.
82-
83-
```XML
84-
<read>
85-
<query name="Gasolines">SELECT NAME FROM GasData</query>
86-
<query name="Oils">SELECT NAME FROM OilData</query>
87-
<query name="GasData">SELECT * FROM GasData</query>
88-
<query name="OilData">SELECT * FROM OilData</query>
89-
</read>
90-
```
91-
92-
* update db.xml with your output data elements mapping to tables.
93-
94-
```XML
95-
<write>
96-
<!-- This maps the output dataset "Result" to the "result" table -->
97-
<table name="Result" target="result"/>
98-
</write>
99-
```
100-
* Initialize a new <code>JdbcCustomDataSource</code>, read your database
101-
configuration file and add your data source to OPL using
102-
<code>IloOplModel.addDataSource()</code>.
103-
104-
```Java
105-
JdbcConfiguration jdbcProperties = null;
106-
String jdbcConfigurationFile = cl.getPropertiesFileName();
107-
if (jdbcConfigurationFile != null) {
108-
jdbcProperties = new JdbcConfiguration();
109-
jdbcProperties.read(jdbcConfigurationFile);
110-
// Create the custom JDBC data source
111-
IloOplDataSource jdbcDataSource = new JdbcCustomDataSource(jdbcProperties, oplF, def);
112-
// Pass it to the model.
113-
opl.addDataSource(jdbcDataSource);
114-
}
115-
```
11666

11767
## Export plain dat files
11868
* When running the `ant` command with the DB2/mysql target, simply add `-Dexport=result.dat` on the command line, and it will export all the tuplesets that have been extracted from the database to `result.dat` file.

0 commit comments

Comments
 (0)