epaperaction.java
来自「培训考试系统代码」· Java 代码 · 共 1,100 行 · 第 1/3 页
JAVA
1,100 行
/**
* 从源列表中随机抽取试题放到目的列表中
* @param srcTestIdArr 源试题ID号列表
* @param srcTestScoreArr 源试题分数列表
* @param srcTestIdArr 抽取的个数
* @param desTestIdArr 目的试题ID号列表
* @param desTestIdArr 目的试题分数列表
*/
private void drawOutTest(ArrayList srcTestIdArr,ArrayList srcTestScoreArr,
int count,ArrayList desTestIdArr,ArrayList desTestScoreArr)
{
//定义抽取的对象
Random rdm = new Random();
//源试题列表中试题的个数
int srcCount = srcTestIdArr.size();
//循环进行抽取
for(int i=0; i<count; i++)
{
//随机抽取(按位置)
int loc = rdm.nextInt(srcCount);
//对应插入位置为loc对象到desTestIdArr与desTestScoreArr中
desTestIdArr.add(srcTestIdArr.get(loc));
desTestScoreArr.add(srcTestScoreArr.get(loc));
//从srcTestIdArr与srcTestScoreArr中把位置为loc的对象删除
srcTestIdArr.remove(loc);
srcTestScoreArr.remove(loc);
srcCount = srcCount - 1;
}
}
/**
* 插入试卷信息与属于试卷的试题到数据库中
* @param form struts封装页面请求的数据
* @param res 响应
* @param testIdArrList 试卷的试题ID列表
* @param testScoreArrList 试卷的试题分数列表
* @return 成功返回null,失败返回一ActionForward对象
*/
private ActionForward insertPaperToDB(ActionMapping mapping,ActionForm form,
HttpServletRequest req,
ArrayList testIdArrList,ArrayList testScoreArrList)
{
//从EPaperFrom中获取试卷的基本信息数据
String title = ((EPaperForm)form).getTitle();
String classId = ((EPaperForm)form).getClassId();
int paperType = ((EPaperForm)form).getPaperType();
String paperId = null;
//定义连接的实例
SysDbConn aplcoms = null;
try
{
//得到一个连接的实例
aplcoms = SysConnPool.getInstance().getAplComs();
//调用存储过程P_Agt_EPaperAdd完成试卷基本信息的添加
aplcoms.preparedSP();
aplcoms.setString(1 ,title);
aplcoms.setString(2 ,classId);
aplcoms.setInt(3 ,paperType);
SysDataSet ds1 = aplcoms.csCommonSP("P_Agt_EPaperAdd");
SysRecord rc1 = ds1.getParamSet();
if(rc1==null || rc1.getInt(0) == OperatorFlagCode.OPERATOR_EORROR)
{//添加失败
//定向到错误页面,错误号为EPAPER_ADD_ERROR;
req.setAttribute("errorId",ErrorCode.EPAPER_MAKE_ADDERROR);
return (mapping.findForward("error"));
}
paperId = rc1.getString(1);
boolean addTestFlag = true;
//把试卷的试题插入数据中
int totalScore = 0;
for(int i=0;i<testIdArrList.size();i++)
{
aplcoms.preparedSP();
aplcoms.setString(1 ,paperId);
aplcoms.setInt(2 ,i+1);
aplcoms.setString(3 ,(String)testIdArrList.get(i));
aplcoms.setString(4 ,(String)testScoreArrList.get(i));
SysDataSet ds2 = aplcoms.csCommonSP("P_Agt_EPaperTestAdd");
SysRecord rc2 = ds2.getParamSet();
if(rc2==null || rc2.getInt(0) == OperatorFlagCode.OPERATOR_EORROR)
{//插入试卷的试题有失败
addTestFlag = false;
}
totalScore += Integer.parseInt((String)testScoreArrList.get(i));
}
if(addTestFlag == false)
{//插入试卷的试题有失败
//在数据库中删除所生成的新试卷与该试卷的试题???????
//定向到错误页面,错误号为EPAPER_ADD_ERROR;
req.setAttribute("errorId",ErrorCode.EPAPER_MAKE_ADDERROR);
return (mapping.findForward("error"));
}
//更新试卷的总分数
aplcoms.preparedQuery("");
aplcoms.setInt(1,totalScore);
aplcoms.setString(2,paperId);
aplcoms.csCommonQuery("SQL_Agt_EPaperUpdateScore","1","-1");
//重新设置paperID的值与状态值
((EPaperForm)form).setPaperId(paperId);
((EPaperForm)form).setStatus(0);
return null;
}
catch (SysDbException aple)
{//捕获CommonService系统异常,定向到出错页面
//输出异常信息
aple.printStackTrace();
req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
return (mapping.findForward("error"));
}
catch(java.sql.SQLException sqle)
{//捕获调用aplcoms异常,定向到出错页面
// 输出异常信息
sqle.printStackTrace();
req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
return (mapping.findForward("error"));
}
catch(Exception e)
{//捕获未知异常,定向到出错页面
//输出异常信息
e.printStackTrace();
req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
return (mapping.findForward("error"));
}
finally
{//关闭连接实例
if(aplcoms != null)
{
aplcoms.close();
}
}
}
/**
* 修改试卷操作
*/
private ActionForward modifyPaper(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
//从EPaperFrom中获取试卷的基本信息数据
String opeType = ((EPaperForm)form).getOpeType();
String paperId = ((EPaperForm)form).getPaperId();
String title = ((EPaperForm)form).getTitle();
String classId = ((EPaperForm)form).getClassId();
int paperType = ((EPaperForm)form).getPaperType();
int status = 0;
if(opeType.equalsIgnoreCase(OperatorFlagCode.EPAPER_MODIFY_RELEASE))
{//编辑并发布试题
status = 1;
}
//从EPaperFrom中获取试卷的试题ID与对应每道题的分数
String[] testIds = ((EPaperForm)form).getTestIds();
String[] testScores = ((EPaperForm)form).getTestScores();
//定义连接的实例
SysDbConn aplcoms = null;
try
{
//得到一个连接的实例
aplcoms = SysConnPool.getInstance().getAplComs();
//调用存储过程P_Agt_EPaperMod完成试卷基本信息的修改与删除试卷的试题,为插入新的试题做准备
aplcoms.preparedSP();
aplcoms.setString(1 ,paperId);
aplcoms.setString(2 ,title);
aplcoms.setString(3 ,classId);
aplcoms.setInt(4 ,paperType);
aplcoms.setInt(5 ,status);
SysDataSet ds1 = aplcoms.csCommonSP("P_Agt_EPaperMod");
SysRecord rc1 = ds1.getParamSet() ;
if(rc1==null || rc1.getInt(0) == OperatorFlagCode.OPERATOR_EORROR)
{//修改失败
//定向到错误页面,错误号为EPAPER_MOD_ERROR;
req.setAttribute("errorId",ErrorCode.EPAPER_MOD_ERROR);
return (mapping.findForward("error"));
}
int totalScore = 0;
boolean addTestFlag = true;
//重新插入属于试卷的试题
for(int i=0;i<testIds.length;i++)
{
aplcoms.preparedSP();
aplcoms.setString(1 ,paperId);
aplcoms.setInt(2 ,i+1);
aplcoms.setString(3 ,(String)testIds[i]);
aplcoms.setString(4 ,(String)testScores[i]);
SysDataSet ds2 = aplcoms.csCommonSP("P_Agt_EPaperTestAdd");
SysRecord rc2 = ds2.getParamSet();
if(rc2==null || rc2.getInt(0) == OperatorFlagCode.OPERATOR_EORROR)
{//插入试卷的试题有失败
addTestFlag = false;
}
totalScore += Integer.parseInt((String)testScores[i]);
}
if(addTestFlag == false)
{
//定向到错误页面,错误号为EPAPER_MOD_ERROR;
req.setAttribute("errorId",ErrorCode.EPAPER_MOD_ERROR);
return (mapping.findForward("error"));
}
//调用queryEpaper函数进行重新查询后返回到列表页面
//return queryPolicy(mapping,form,req,res);
//更新试卷的总分数
aplcoms.preparedQuery("");
aplcoms.setInt(1,totalScore);
aplcoms.setString(2,paperId);
aplcoms.csCommonQuery("SQL_Agt_EPaperUpdateScore","1","-1");
//提示操作成功
if(status == 0)
{
req.setAttribute("successId",SuccessCode.EPAPER_MODIFY_SUCCESS);
}
else
{
req.setAttribute("successId",SuccessCode.EPAPER_RELEASE_SUCCESS);
}
return (mapping.findForward("success"));
}
catch (SysDbException aple)
{//捕获CommonService系统异常,定向到出错页面
//输出异常信息
aple.printStackTrace();
req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
return (mapping.findForward("error"));
}
catch(java.sql.SQLException sqle)
{//捕获调用aplcoms异常,定向到出错页面
// 输出异常信息
sqle.printStackTrace();
req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
return (mapping.findForward("error"));
}
catch(Exception e)
{//捕获未知异常,定向到出错页面
//输出异常信息
e.printStackTrace();
req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
return (mapping.findForward("error"));
}
finally
{//关闭连接实例
if(aplcoms != null)
{
aplcoms.close();
}
}
}
/**
* 删除试卷操作
*/
private ActionForward deletePaper(ActionMapping mapping,
ActionForm form, HttpServletRequest req,
HttpServletResponse res)
{
//获取操作类型
String opeType =((EPaperForm)form).getOpeType();//从form中获取
//从EPaperForm中获取试卷ID号
//按组删除
String[] paperIds = ((EPaperForm)form).getPaperIds();
//按单个删除
if(paperIds == null)
{
paperIds = new String[1];
paperIds[0] = new String(((EPaperForm)form).getPaperId());
}
//以"#"号组合所要删除的试卷,之间以"#"号分割
String paperIdSet = "";
paperIdSet = Tools.getStringSet(paperIds);
//定义连接的实例
SysDbConn aplcoms = null;
try
{
//得到一个连接的实例
aplcoms = SysConnPool.getInstance().getAplComs();
//调用存储过程 P_Agt_EPaperDel完成试卷的删除
aplcoms.preparedSP();
aplcoms.setString(1 ,paperIdSet);
SysDataSet ds = aplcoms.csCommonSP("P_Agt_EPaperDel");
SysRecord rc = ds.getParamSet() ;
if(rc==null || rc.getInt(0) == OperatorFlagCode.OPERATOR_EORROR)
{//删除试卷失败
//定向到错误页面,错误号为EPAPER_DELETE_ERROR;
req.setAttribute("errorId",ErrorCode.EPAPER_DELETE_ERROR);
return (mapping.findForward("error"));
}
//删除试卷部分失败
if(rc.getString(1) != null && rc.getString(1).length()>10)
{
req.setAttribute("errorId",ErrorCode.EPAPER_PARTDELERROR);
req.setAttribute("errorMes",rc.getString(1));
return (mapping.findForward("error"));
}
//调用queryEpaper函数进行重新查询后返回到列表页面
if(opeType.equalsIgnoreCase(OperatorFlagCode.EPAPER_DELETE))
{
return queryPaper(mapping,form,req,res);
}
else
{ //成功提示
req.setAttribute("successId",SuccessCode.EPAPER_DELETE_SUCCESS);
req.setAttribute("backURL","/ExamPaperMan.do?opeType=1");
return (mapping.findForward("success"));
}
}
catch (SysDbException aple)
{//捕获CommonService系统异常,定向到出错页面
//输出异常信息
aple.printStackTrace();
req.setAttribute("errorId",ErrorCode.COMMONSERVICE_ERROR);
return (mapping.findForward("error"));
}
catch(java.sql.SQLException sqle)
{//捕获调用aplcoms异常,定向到出错页面
// 输出异常信息
sqle.printStackTrace();
req.setAttribute("errorId",ErrorCode.DATABASE_ERROR);
return (mapping.findForward("error"));
}
catch(Exception e)
{//捕获未知异常,定向到出错页面
//输出异常信息
e.printStackTrace();
req.setAttribute("errorId",ErrorCode.UNKNOW_ERROR);
return (mapping.findForward("error"));
}
finally
{//关闭连接实例
if(aplcoms != null)
{
aplcoms.close();
}
}
}
/**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?