|
| 1 | +# Example with integration in CPLEX Optimization Studio IDE |
| 2 | + |
| 3 | + |
| 4 | +## Setup |
| 5 | + |
| 6 | +Before you run the sample, please make sure you created an example database. |
| 7 | +See the *Setup the sample database* section for [DB2](../../README.DB2.md), |
| 8 | +[MySQL](../../README.MySQL.md), [Microsoft SQL Server](../../README.SQLServer.md). |
| 9 | + |
| 10 | +Then, you will need to edit [jdbc.js](jdbc.js), and configure paths. |
| 11 | +In particular, you want to add the JDBC connector jar. For convenience you can |
| 12 | +also just set environment variables OPL_JDBC_DRIVER to point to your driver location |
| 13 | +and OPL_JDC_LIBS to the directory containing `jdbc-custom-data-source.jar` |
| 14 | + |
| 15 | +``` |
| 16 | +// OPL_JDBC_DRIVER points to the jar for the jdbc driver you want to use. |
| 17 | +var jdbc_driver = IloOplGetEnv("OPL_JDBC_DRIVER"); |
| 18 | +if (! jdbc_driver ) { |
| 19 | + jdbc_driver = "../../external_libs/mssql-jdbc-7.2.2.jre8.jar"; // default for this project |
| 20 | +} |
| 21 | +
|
| 22 | +// OPL_JDBC_LIBS points to the directory containing the library needed for this sample. |
| 23 | +// You want to put jdbc-custom-data-source.jar there. |
| 24 | +var libs = IloOplGetEnv("OPL_JDBC_LIBS"); |
| 25 | +if (! libs ) { |
| 26 | + libs = "../../lib"; // default value use the lib at the root of this project |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Data definition |
| 31 | + |
| 32 | +The data input definition relies on support functions defined in `jdbc.js`. |
| 33 | + |
| 34 | +You include that script using: |
| 35 | + |
| 36 | +``` |
| 37 | + includeScript("jdbc.js"); |
| 38 | +``` |
| 39 | + |
| 40 | +Then use the defined functions to create a connector and define inputs: |
| 41 | + |
| 42 | +``` |
| 43 | + // Create the jdbc custom data source |
| 44 | + var db = JDBCConnector("jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;databaseName=custom_data_source;integratedSecurity=true"); |
| 45 | + |
| 46 | + // input data |
| 47 | + db.read("Gasolines", "SELECT NAME FROM GASDATA"); |
| 48 | + db.read("Oils", "SELECT NAME FROM OILDATA"); |
| 49 | + db.read("GasData", "SELECT * FROM GASDATA"); |
| 50 | + db.read("OilData", "SELECT * FROM OILDATA"); |
| 51 | + |
| 52 | + // create result table |
| 53 | + db.execute("DROP TABLE result"); |
| 54 | + db.execute("CREATE TABLE result(oil VARCHAR(30), gas VARCHAR(30), blend FLOAT, a FLOAT)"); |
| 55 | + |
| 56 | + // Formerly: items to DBUpdate(db, ) |
| 57 | + db.update("items", "INSERT INTO result(oil, gas, blend, a) VALUES (?,?,?,?)"); |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +## Running the sample |
| 62 | + |
| 63 | +Import the project in your studio. Create a new *Run Configuration*. |
| 64 | +In the Projects view, drag and drop the `integration.mod` and `integration.dat` to the configuration. |
| 65 | +You are now ready to run the sample in the studio. |
0 commit comments