This post shows how to connect to DB in Java compute node & how to construct output message from database result set.
Below diagram shows the database table :
Schema :
Here is the evaluate method :
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbMessage inMessage = inAssembly.getMessage();
MbMessage outMessage = new MbMessage();
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,outMessage);
try {
copyMessageHeaders(inMessage, outMessage);
MbElement inRootElement = inMessage.getRootElement();
List<MbElement> idList = (List) inRootElement.getLastChild().evaluateXPath("/ID");
int id = (Integer)idList.get(0).getValue();
//Creating Output data
MbElement outRootElement = outMessage.getRootElement();
MbElement messageType = outRootElement.getFirstChild().getFirstElementByPath("MessageType");
messageType.setValue("Response");
MbElement parser = outRootElement.createElementAsLastChild("MRM");
// Conventional way of connecting to DB.
Class.forName("com.ibm.db2.jcc.DB2Driver"); //Driver for DB2
String url = "jdbc:db2://localhost:50000/TEST"; // Database name : TEST
Connection conn = DriverManager.getConnection(url,"username","password");
//Connection conn = getJDBCType4Connection("MYDB",JDBC_TransactionType.MB_TRANSACTION_AUTO);
// For this need to configure the configurable service.
Statement stmt = conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = stmt.executeQuery("SELECT COMPANY, SALARY FROM ADMIN.EMP WHERE ID ="+id);
while(resultSet.next())
{
parser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"COMPANY",resultSet.getString("COMPANY"));
parser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"SALARY",resultSet.getInt("SALARY"));
}
out.propagate(outAssembly);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
// clear the outMessage even if there's an exception
outMessage.clearMessage();
}
}
Input :
<Request><ID>834</ID></Request>
Output :
<?xml version="1.0"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<COMPANY>GALAXY</COMPANY>
<SALARY>900000</SALARY>
</Response>
Below diagram shows the database table :
Database (TEST) Schema ( ADMIN) Table (EMP) |
Schema :
mxsd |
Here is the evaluate method :
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbMessage inMessage = inAssembly.getMessage();
MbMessage outMessage = new MbMessage();
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,outMessage);
try {
copyMessageHeaders(inMessage, outMessage);
MbElement inRootElement = inMessage.getRootElement();
List<MbElement> idList = (List) inRootElement.getLastChild().evaluateXPath("/ID");
int id = (Integer)idList.get(0).getValue();
//Creating Output data
MbElement outRootElement = outMessage.getRootElement();
MbElement messageType = outRootElement.getFirstChild().getFirstElementByPath("MessageType");
messageType.setValue("Response");
MbElement parser = outRootElement.createElementAsLastChild("MRM");
// Conventional way of connecting to DB.
Class.forName("com.ibm.db2.jcc.DB2Driver"); //Driver for DB2
String url = "jdbc:db2://localhost:50000/TEST"; // Database name : TEST
Connection conn = DriverManager.getConnection(url,"username","password");
//Connection conn = getJDBCType4Connection("MYDB",JDBC_TransactionType.MB_TRANSACTION_AUTO);
// For this need to configure the configurable service.
Statement stmt = conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = stmt.executeQuery("SELECT COMPANY, SALARY FROM ADMIN.EMP WHERE ID ="+id);
while(resultSet.next())
{
parser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"COMPANY",resultSet.getString("COMPANY"));
parser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"SALARY",resultSet.getInt("SALARY"));
}
out.propagate(outAssembly);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
// clear the outMessage even if there's an exception
outMessage.clearMessage();
}
}
Input :
<Request><ID>834</ID></Request>
Output :
<?xml version="1.0"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<COMPANY>GALAXY</COMPANY>
<SALARY>900000</SALARY>
</Response>
what is MRM?
ReplyDeleteMessage Repository manager,if we want raw formats transformation like csv, we need a schema to do such transformations,its possible using MRM or DFDL
Delete.CSV is a physical format which is not understandable by IIB,so we need to create a logical format using DFDL or MRM.(Physical to logical conversion is Modeling which can be done by MRM or DFDL)
Thanks for the guidance.
ReplyDeleteOne question, do we really need to pass userid/password for connection object here? Can't we use the DSN name to connect here also like ESQLs?
Any input/suggestion will help.
As per my knowledge using dsn follows type 1 driver which is native lib dependant so it's better to use jdbc type 4 driver.
DeleteYou do not have to use user/password in the code. You can use 'mqsisetdbparms' to set user/password for the configured service name.
ReplyDeleteHello,where do we keep the lib jars say Oracle driver jar.
DeletePlease advice
Thanks for the sample code with setup ,it really helped me to quickly understand.
ReplyDeleteCan we get some more samples with design best practices eg creating a reusable db connection code ,
ReplyDeleteHi,
ReplyDeleteI am also using Java compute node to connect to MSSQL database using JDBC connectivity. Used above process for connection however i am getting this error:
BIP6265
A problem was encountered when committing a transaction with the JDBC Datasource .
Severity
20 : Error
Explanation
The node was unable to commit a database transaction with DataSource , which represents a JDBC provider. This problem can be caused by network problems, or errors on the database server. The transaction might be left in an "in-doubt" state, which you might have to resolve manually.
Response
Check the database logs to determine the reason for the problem, and refer to the documentation supplied by the database provider. Contact your IBM support center if you are unable to determine the cause of the problem.
Any help will be appreciated.