调用具有3个输出参数的存储过程时出错。我正在使用Tomcat JNDI配置建立连接。
以下是我在java中的代码。
package com.ef.clients.ptcl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.audium.server.AudiumException;
import com.audium.server.session.ActionElementData;
import com.audium.server.voiceElement.ActionElementBase;
public class GetCallerCountNew extends ActionElementBase {
DataSource ds;
public GetCallerCountNew(){
Context initialContext;
try{
initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("jdbc/CallerCount");
}catch(Exception ex){
}
}
CallableStatement proc = null;
Connection connection = null;
String ani = null;
String dnis = null;
@Override
public void doAction(String arg0, ActionElementData data) throws AudiumException {
try {
long time_1 = System.currentTimeMillis();
data.addToLog("Entered in getCallerCount class:",null);
ani = (String) data.getSessionData("ani");
dnis = (String) data.getSessionData("dnis");
//going to get connection.
if (ds == null) {
data.addToLog("DataSource isn't established.", null);
throw new AudiumException("DataSource isn't established.");
}
connection = ds.getConnection();
if (connection == null) {
data.addToLog("Connection isn't established.", null);
throw new AudiumException("Connection isn't established.");
}
// Reading Caller Count
int WirelineCountToday;
int WireLessCountToday;
int TotalCountToday;
proc = null;
proc = connection.prepareCall("{ call Custom_Db_Expertqueue_GetCallerCount(?,?,?,?,?) }");
data.addToLog("Setting ANI: ",ani);
String parameters = "ANI: " + ani + " DNIS: " + dnis;
data.addToLog("Calling Stroed proceedure with parameters . ", parameters);
proc.setString(1, ani);
proc.setString(2, dnis);
proc.registerOutParameter(3, java.sql.Types.INTEGER);
proc.registerOutParameter(4, java.sql.Types.INTEGER);
proc.registerOutParameter(5, java.sql.Types.INTEGER);
data.addToLog("Stroed proceedure executed successfully. ", null);
int out = proc.executeUpdate();
// getting output parameters from strored proceedure
WirelineCountToday=proc.getInt(3);// getInt(1);
WireLessCountToday=proc.getInt(4);
TotalCountToday=proc.getInt(5);
// setting session values
data.setSessionData("WirelineCount", WirelineCountToday);
data.setSessionData("WirelessCount", WireLessCountToday);
data.setSessionData("TotalCount", TotalCountToday);
data.addToLog("WirelineCountToday Value Retrieved:", Integer.toString(WirelineCountToday));
data.addToLog("TotalCountToday Value Retrieved:", Integer.toString(TotalCountToday));
data.addToLog("WireLessCountToday Value Retrieved:", Integer.toString(WireLessCountToday));
data.addToLog("Returned value from proceedure is:", Integer.toString(out));
data.addToLog("for Information.... SUCCESS = 0, FAILURE=1", null);
long time_2 = System.currentTimeMillis();
data.addToLog("Total Execution Time is: ", time_2-time_1+" ms");
}
catch (Exception e)
{ data.addToLog("Exeption while creating connection: ", e.toString()); }
finally {
data.addToLog("Going to close connection and callStatement.", null);
try {
if(proc !=null){
proc.close();
data.addToLog("callStatement Closed Successfully.", null);
}
else{
data.addToLog("callStatement is null..", null);
}
} catch (SQLException e) {
data.addToLog("ERROR WHILE TRYING TO CLOSE STATEMENT. MESSAGE IS: ", e.toString());
}
proc = null;
try {
if(connection != null) {
connection.close();
data.addToLog("Connection closed successfully.", null);
}
else{
data.addToLog("Connection is null..", null);
}
} catch (SQLException e) {
data.addToLog("ERROR WHILE TRYING TO CLOSE CONNECTINO. MESSAGE IS: ", e.toString());
}
connection = null;
}
}
}
以下是存储过程。
ALTER PROCEDURE [dbo].[Custom_Db_Expertqueue_GetCallerCount]
@ANI varchar(50),
@DNIS varchar(50),
@WirelineCount varchar(10)out,
@WirelessCount varchar(10) out,
@TotalCallCount varchar(10) out
AS
BEGIN
IF(@DNIS='45236' OR @DNIS='45412')
BEGIN
SELECT @WirelineCount = COUNT(*) FROM [ptcl_repetitivecaller].[dbo].[Custom_Db_ExpertQueue_CallCount]
Where ANI =@ANI
AND (DNIS= '45236' OR DNIS ='45236') and AGENT_TRANSFER=0
AND (SG IN('PSTN','BB','GPON','IPTV','PnNet','CallCard','VAS'))
AND cast (CALL_TIME as Date) = cast (GETDATE() as DATE)
SELECT @WirelessCount = COUNT(*) FROM [ptcl_repetitivecaller].[dbo].[Custom_Db_ExpertQueue_CallCount]
WHERE ANI = @ANI AND (DNIS='45236' OR DNIS='45236')
AND AGENT_TRANSFER=0
AND (SG IN('Test','WLL','OUTAGE_EVO'))
AND cast (CALL_TIME as Date) = cast (GETDATE() as DATE)
SET @TotalCallCount = cast(@WirelineCount as int) + cast(@WirelessCount as int)
END
ELSE IF(@DNIS='45236' OR @DNIS='45410' OR @DNIS='45218' OR @DNIS='45418')
BEGIN
SELECT @WirelineCount = COUNT(*) FROM [ptcl_repetitivecaller].[dbo].[Custom_Db_ExpertQueue_CallCount]
WHERE ANI = @ANI AND (DNIS='45210' OR DNIS='45410' OR DNIS='45218' OR DNIS='45418')
AND (SG IN('PSTN','BB','WIRELINE','SALES','WIRELINE','GPON'))
AND AGENT_TRANSFER=1
AND cast (CALL_TIME as Date) = cast (GETDATE() as DATE)
SELECT @WirelessCount = COUNT(*) FROM [ptcl_repetitivecaller].[dbo].[Custom_Db_ExpertQueue_CallCount]
WHERE ANI = @ANI AND (DNIS='45210' OR DNIS='45410' OR DNIS='45218' OR DNIS='45418')
AND (SG IN('EVO','WIRLESS','WIRELESS'))
AND AGENT_TRANSFER=1
AND cast (CALL_TIME as Date) = cast (GETDATE() as DATE)
SET @TotalCallCount = cast(@WirelineCount as int) + cast(@WirelessCount as int)
END
END
大部分时间我都会收到上述错误。请帮我解决这个问题。
答案 0 :(得分:0)
您的代码看起来不错。当您尝试在实际执行对它的调用之前访问存储过程的结果时,会发生这种情况。例如,运行此操作会导致您收到的错误:
// getting output parameters from strored proceedure
WirelineCountToday=proc.getInt(3);// getInt(1);
WireLessCountToday=proc.getInt(4);
TotalCountToday=proc.getInt(5);
int out = proc.executeUpdate();
另外,对于select语句,我建议使用:
int out = proc.execute();
答案 1 :(得分:0)
在我的情况下,输出值获取的是NULL而不是大的int值(由于查询中没有映射到输出变量的记录)。因此,在存储过程结束时,我检查了输出值是否为null并将其分配给像-1这样的int值。