Topic
No replies
SystemAdmin
SystemAdmin
53 Posts
ACCEPTED ANSWER

Pinned topic Unable to fetch database in eforms using javascript and jsp

‏2012-05-04T11:41:09Z |
I made an eForm that looksup the database for values based on indent number in the eform and upload those values to populate eform fields at runtime. Fot it I made a JSP file called MyJSP.jsp to populate fields and create database connection in value.jsp and added it to regions folder in workplaceXT and added it to regions options in step processor also.But it isn't working while I open the eform in WorkplaceXT.The code I have writen is as follows:

1)MYJSP.jsp

<html>
<head>
<script language="JavaScript" src="../forms/misc/eforms-factory.js">
var xmlHttp;
form.addOnLoad(initHandler);

function initHandler() {
form.findCell("Indent_No").OnChange.add(getTableDataFromDB);
}

function GetXmlHttpObject() {
xmlHttp=null;

try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();

} catch (e) {
//Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

function getTableDataFromDB() {
var Indent_No="47893";
xmlHttp=GetXmlHttpObject();
var url="Value.jsp";
url += "?Indent_No=" +Indent_No;
xmlHttp.open("GET",url, true);
xmlHttp.onreadystatechange = setTableDataOnForm;
xmlHttp.send(null);
}

function setTableDataOnForm() {

if (xmlHttp.readyState == 4) {

if (xmlHttp.status == 200) {
var respText = xmlHttp.responseText;
var token = respText.split( ";" );

for (i=0; i<token.length; i++) {
var respTokens = token[i].split( "|" );
form.findCell("Item_Description").setValueFromString(respTokens[0]);
form.findCell("Item_Id").setValueFromString(respTokens[1]);
form.findCell("Quantity").setValueFromString(respTokens[2]);
form.findCell("UOM").setValueFromString(respTokens[3]);
form.findCell("Need_By_Date").setValueFromString(respTokens[4]);
}
}
}
}

</script>
</head>
<body class="sidebar">
</body>
</html>
2)Value.jsp

<%@page language="java" import ="java.sql.*" %>
<%
try{
String name="47893";
String sb="";
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:db2://192.168.1.55:50000/P8_DB:retrieveMessagesFromServerOnGetMessage=true","P8Admin","Admin123");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select Item_Description,Item_ID,Quantity,UOM,Need_By_Date from P8Admin.ITEMS_Data where Indent_No="+name);

while(rs.next()) {

for(int i=0;i<5;i++) {
sb = rs.getString(i)"|";
out.print(sb);
}
sb += ";";
}
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(sb);

} catch(Exception ex) {
out.println(ex.getMessage()+" \n Have Problems in accessing database");
}
%>

Could anyone please help me in resolving the issue. the screenshot of eform is attached: