aspose file tools*
The moose likes Servlets and the fly likes Calling COBOL stored proc from Java Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling COBOL stored proc from Java Servlet" Watch "Calling COBOL stored proc from Java Servlet" New topic
Author

Calling COBOL stored proc from Java Servlet

Donna Reschke
Greenhorn

Joined: Jul 05, 2001
Posts: 23
I am trying to call a COBOL stored proc from a Java Servlet. The stored proc is stored on a DB2 database. I need to send 6 inputs to the COBOL stored proc and the output will be the return code of the stored proc. I'm not sure if I'm going about this the right way. This is how my code looks...
public int callStoredProc(CallableStatement cstmt,
Connection con,
String sYear,
String sReportNbr,
String sSystemCode,
String sUserId,
String sModuleNbr,
String sFormId){
int iParm1 = 0;
try{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
System.out.println("Failed to locate database driver: "
+ ex.toString());
return iParm1;
}
try{
cstmt = con.prepareCall("{? = CALL MKTPZ90C
(?, ?, ?, ?, ?, ?)}");
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.setString(2, sYear);
cstmt.setString(3, sReportNbr);
cstmt.setString(4, sSystemCode);
cstmt.setString(5, sUserId);
cstmt.setString(6, sModuleNbr);
cstmt.setString(7, sFormId);
cstmt.execute();
iParm1 = cstmt.getInt(1);
CloseSQLStatement(cstmt);
}
catch(SQLException ex) {
CloseSQLStatement(cstmt);
System.out.println("SQL exception occurred:" +
ex.toString());
return iParm1;
}
return iParm1;
}
Could someone tell me if this is the right way to go about doing this?
Thanks!!!
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
This looks about right -- now I have two questions:
(1) Why are you using the ODBC bridge rather than the native DB2 JDBC drivers (which are WAY faster)
(2) Have you tested this? If so, did you get any errors?
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Donna Reschke
Greenhorn

Joined: Jul 05, 2001
Posts: 23
Could you post a code example of using the native DB2 JDBC drivers? If I already have a connection established in my servlet, and I pass the con variable to my method, wouldn't this work just as well? I have not yet tested this, I'll be doing that tomorrow. First I want to test the stored procedure to make sure it is running smoothly.
Thanks for your input.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
OK, one more question and then I'll post code, I promise
Are you running this in WebSphere or Tomcat, or some other Servlet engine?
(Really, there's a reason for this question -- I have to know how to tell you how to best obtain the connection for the DB2 driver).
Kyle
One more thing -- the "Class.forName()" business only needs to happen ONCE in your entire program -- if you're being passed a connection you probably already have done this, so you don't need it.
[ February 11, 2002: Message edited by: Kyle Brown ]
Donna Reschke
Greenhorn

Joined: Jul 05, 2001
Posts: 23
We are using JRun to run our servlets. Thanks for your help.
Donna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling COBOL stored proc from Java Servlet