Skip to content

Commit 54ec828

Browse files
authored
Merge pull request #9 from vlkong/master
Use PrepareStatements and transaction for insertion in database
2 parents ad6ed99 + a6ff7ef commit 54ec828

File tree

2 files changed

+59
-41
lines changed

2 files changed

+59
-41
lines changed

lib/jdbc-custom-data-source.jar

3.28 KB
Binary file not shown.

src/main/java/com/ibm/opl/customdatasource/JdbcWriter.java

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.sql.Connection;
1414
import java.sql.DriverManager;
15+
import java.sql.PreparedStatement;
1516
import java.sql.SQLException;
1617
import java.sql.Statement;
1718
import java.util.Enumeration;
@@ -79,37 +80,38 @@ else if (type == Type.STRING)
7980

8081
static final String INSERT_QUERY = "INSERT INTO %(";
8182

82-
String insertQuery(IloTupleSchema schema, String table) {
83-
String query = INSERT_QUERY.replace("%", table);
84-
for (int i = 0; i < schema.getSize(); i++) {
85-
String columnName = schema.getColumnName(i);
86-
query += columnName;
87-
if (i < (schema.getSize() - 1))
88-
query += ", ";
89-
}
90-
query += ") VALUES(%)";
91-
return query;
83+
String getPlaceholderString(int size) {
84+
StringBuffer b = new StringBuffer();
85+
for (int i=0; i < size-1; i++)
86+
b.append("?,");
87+
b.append("?");
88+
return b.toString();
9289
}
93-
94-
// create the inserting value
95-
String insertValueString(IloTuple tuple, IloTupleSchema schema) {
96-
IloOplElementDefinition tupleDef = _def.getElementDefinition(schema.getName());
97-
IloOplTupleSchemaDefinition tupleSchemaDef = tupleDef.asTupleSchema();
98-
String values = "";
99-
for (int i = 0; i < schema.getSize(); i++) {
100-
String value = "";
101-
Type columnType = tupleSchemaDef.getComponent(i).getElementDefinitionType();
102-
if (columnType == Type.INTEGER)
103-
value = Integer.toString(tuple.getIntValue(i));
104-
else if (columnType == Type.FLOAT)
105-
value = Double.toString(tuple.getNumValue(i));
106-
else if (columnType == Type.STRING)
107-
value = "'" + tuple.getStringValue(i) + "'";
108-
values += value;
109-
if (i < (schema.getSize() - 1))
110-
values += ", ";
111-
}
112-
return values;
90+
91+
String getInsertQuery(IloTupleSchema schema, String table) {
92+
String query = INSERT_QUERY.replace("%", table);
93+
for (int i = 0; i < schema.getSize(); i++) {
94+
String columnName = schema.getColumnName(i);
95+
query += columnName;
96+
if (i < (schema.getSize() - 1))
97+
query += ", ";
98+
}
99+
query += ") VALUES(" + getPlaceholderString(schema.getSize()) + ")";
100+
return query;
101+
}
102+
103+
void updateValues(IloTuple tuple, IloTupleSchema schema,
104+
IloOplTupleSchemaDefinition tupleSchemaDef, PreparedStatement stmt) throws SQLException {
105+
for (int i = 0; i < schema.getSize(); i++) {
106+
int index = i+1; // index in PreparedStatement
107+
Type columnType = tupleSchemaDef.getComponent(i).getElementDefinitionType();
108+
if (columnType == Type.INTEGER)
109+
stmt.setInt(index, tuple.getIntValue(i));
110+
else if (columnType == Type.FLOAT)
111+
stmt.setDouble(index, tuple.getNumValue(i));
112+
else if (columnType == Type.STRING)
113+
stmt.setString(index, tuple.getStringValue(i));
114+
}
113115
}
114116

115117
static final String DROP_QUERY = "DROP TABLE %";
@@ -122,9 +124,9 @@ void customWrite(String name, String table) {
122124
IloOplElement elt = _model.getElement(name);
123125
ilog.opl_core.cppimpl.IloTupleSet tupleSet = (ilog.opl_core.cppimpl.IloTupleSet) elt.asTupleSet();
124126
IloTupleSchema schema = tupleSet.getSchema_cpp();
125-
127+
Connection conn = null;
126128
try {
127-
Connection conn = DriverManager.getConnection(_configuration.getUrl(), _configuration.getUser(),
129+
conn = DriverManager.getConnection(_configuration.getUrl(), _configuration.getUser(),
128130
_configuration.getPassword());
129131
Statement stmt = conn.createStatement();
130132
String sql;
@@ -141,18 +143,34 @@ void customWrite(String name, String table) {
141143
sql = createTableQuery(schema, table);
142144
// System.out.println(sql);
143145
stmt.executeUpdate(sql);
144-
// then insert values
145-
sql = insertQuery(schema, table);
146-
// iterate the set and create the final insert statement
147-
for (java.util.Iterator it1 = tupleSet.iterator(); it1.hasNext();) {
148-
IloTuple tuple = (IloTuple) it1.next();
149-
String finalsql = sql.replaceFirst("%", insertValueString(tuple, schema));
150-
// System.out.println(finalsql);
151-
stmt.executeUpdate(finalsql);
146+
147+
try {
148+
IloOplElementDefinition tupleDef = _def.getElementDefinition(schema.getName());
149+
IloOplTupleSchemaDefinition tupleSchemaDef = tupleDef.asTupleSchema();
150+
151+
conn.setAutoCommit(false); // begin transaction
152+
String psql = getInsertQuery(schema, table);
153+
PreparedStatement pstmt = conn.prepareStatement(psql);
154+
// iterate the set and create the final insert statement
155+
for (java.util.Iterator it1 = tupleSet.iterator(); it1.hasNext();) {
156+
IloTuple tuple = (IloTuple) it1.next();
157+
updateValues(tuple, schema, tupleSchemaDef, pstmt);
158+
pstmt.executeUpdate();
159+
}
160+
conn.commit();
161+
} catch (SQLException e) {
162+
conn.rollback();
152163
}
164+
153165
} catch (SQLException e) {
154-
// TODO Auto-generated catch block
155166
e.printStackTrace();
167+
} finally {
168+
if (conn != null)
169+
try {
170+
conn.close();
171+
} catch (SQLException e) {
172+
e.printStackTrace();
173+
}
156174
}
157175
}
158176
}

0 commit comments

Comments
 (0)