📄 questiondao.java
字号:
package edu.yinhe.mis.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import edu.yinhe.mis.dto.DifficultyDegreeDTO;
import edu.yinhe.mis.dto.QuestionDTO;
import edu.yinhe.mis.vo.DictionaryVO;
import edu.yinhe.mis.vo.DifficultyDegreeVO;
import edu.yinhe.mis.vo.OutlineVO;
import edu.yinhe.mis.vo.QuestionVO;
import edu.yinhe.system.model.BaseDAO;
import edu.yinhe.system.model.IBaseDAO;
/**
* @author 石小军
* @since 2008-04-30
*
*/
public class QuestionDAO extends BaseDAO {
/**
* @author 石小军
* 删除试题
* @return boolean (删除成功返回true)
*/
public Object delete(Object obj) throws SQLException {
Integer n = -1;
Integer m = 0;
String sql = "";
StringBuffer sb = new StringBuffer();
String[] str = new String[2];
PreparedStatement ps = null;
try {
str = (String[])obj;
System.out.println(str.length);
for(int i=0;i<str.length;i++){
sb.append("'")
.append(str[i])
.append("',");
}
System.out.println(sb.substring(0,sb.length()));
sql = "DELETE FROM question WHERE QUESTION_ID IN ("+sb.substring(0,sb.length()-1)+")";
ps = conn.prepareStatement(sql);
n = ps.executeUpdate();
if(n!=-1){
m = 1;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null)ps.close();
sql = null;
}
return m;
}
/**
* @author 石小军
* 查询难度等级表中所有的信息
* @return list(封装了所有的试题)
*
*/
public Object find() throws SQLException {
ArrayList list = new ArrayList();
String sql = "";
DifficultyDegreeVO vo = null;
DifficultyDegreeDTO dto = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
sql = "SELECT d.difficultyDegree_ID,d.difficultyDegree_Name FROM difficultydegree d";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
vo = new DifficultyDegreeVO();
vo.setDifficultyDegreeID(rs.getInt("difficultyDegree_ID"));
vo.setDifficultyDegreeName(rs.getString("difficultyDegree_Name"));
list.add(vo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps = null;
if(rs!=null) ps = null;
sql = null;
vo = null;
}
return list;
}
public Object find(Object obj) throws SQLException {
return null;
}
/**
* @author 石小军
* 查询所有的科目,并放入到List中!
* @return list(封装了所有的科目)
*
*/
public Object findAll() throws SQLException {
String sql = "";
ArrayList list = new ArrayList();
DictionaryVO vo = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
sql = "SELECT dt.name FROM dictionary dt where scope='9'";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
vo = new DictionaryVO();
vo.setName(rs.getString("name"));
list.add(vo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps = null;
if(rs!=null) ps = null;
sql = null;
vo = null;
}
return list;
}
/**
* @author 石小军
* 查询所有的试题,并放入到List中!
* @return list(封装了所有的试题)
*
*/
public Object findAll(Object obj) throws SQLException {
String sql = "";
QuestionVO vo = null;
Object[] objq = new Object[2];
QuestionDTO dto = (QuestionDTO)obj;
ArrayList list = new ArrayList();
Integer integer = new Integer(0);
PreparedStatement ps = null;
ResultSet rs = null;
StringBuffer sb = new StringBuffer("select q.question_id,q.question_name,q.question_result,q.DIFFICULTYDEGREE_ID,d.DIFFICULTYDEGREE_ID,d.DIFFICULTYDEGREE_name,q.questiontype_id,qt.questiontype_id,qt.questiontype_name,q.object_name,q.explains,q.option_a,q.option_b,q.option_c,q.option_d from question q,difficultydegree d,questiontype qt where q.questiontype_id=qt.questiontype_id and q.difficultydegree_id=d.difficultydegree_id and 1=1");
try {
integer = getCount(dto);
if(dto.getQuestionID()!=0&&!"".equals(dto.getQuestionID())){
sb.append(" AND QUESTION_ID='"+dto.getQuestionID()+"'");
}
if(dto.getQuestionName() != null&&!"".equals(dto.getQuestionName())){
sb.append(" AND QUESTION_NAME LIKE '%"+dto.getQuestionName()+"%'");
}
if(dto.getCurrentPage() != 0&&!"".equals(dto.getCurrentPage())&&dto.getRowPerPage()!=0&&!"".equals(dto.getRowPerPage())){
int i = ((dto.getCurrentPage())-1)*(dto.getRowPerPage());
sb.append(" LIMIT "+i+", "+dto.getRowPerPage());
}
sql = sb.toString();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
vo = new QuestionVO();
vo.setQuestionID(rs.getInt("question_ID"));
vo.setQuestionName(rs.getString("question_Name"));
vo.setQuestionResult(rs.getString("question_Result"));
vo.setQuestionTypeID(rs.getInt("questionType_ID"));
vo.setQuestionTypeName(rs.getString("questionType_Name"));
vo.setDifficultyDegreeID(rs.getInt("difficultyDegree_ID"));
vo.setDifficultyDegreeName(rs.getString("difficultyDegree_Name"));
vo.setExplains(rs.getString("explains"));
vo.setObjectName(rs.getString("object_Name"));
vo.setOptionA(rs.getString("option_A"));
vo.setOptionB(rs.getString("option_B"));
vo.setOptionC(rs.getString("option_C"));
vo.setOptionD(rs.getString("option_D"));
list.add(vo);
}
objq[0] = list;
objq[1] = integer;
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps.close();
if(rs!=null) rs.close();
dto = null;
sql = null;
}
return objq;
}
/**
* @author 石小军
* 根据试题ID号查询该试题
* @return vo (vo中封装了该ID试题的所有详细信息)
*/
public Object findById(Object obj) throws SQLException {
Integer m = 0;
QuestionVO vo = null;
ResultSet rs = null;
PreparedStatement ps = null;
String sql = null;
try {
m = (Integer)obj;
sql = "SELECT q.QUESTION_ID,q.QUESTION_NAME,q.QUESTION_RESULT,q.DIFFICULTYDEGREE_ID,d.DIFFICULTYDEGREE_NAME," +
"q.QUESTIONTYPE_ID,qt.QUESTIONTYPE_NAME,q.OBJECT_NAME,q.EXPLAINS,q.OPTION_A," +
"q.OPTION_B,q.OPTION_C,q.OPTION_D FROM question q,difficultydegree d,questiontype qt" +
" WHERE q.QUESTIONTYPE_ID=qt.QUESTIONTYPE_ID AND q.DIFFICULTYDEGREE_ID=d.DIFFICULTYDEGREE_ID " +
" AND QUESTION_ID=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, m);
rs = ps.executeQuery();
while(rs.next()){
vo = new QuestionVO();
vo.setQuestionID(rs.getInt("question_ID"));
vo.setQuestionName(rs.getString("question_Name"));
vo.setQuestionResult(rs.getString("question_Result"));
vo.setQuestionTypeID(rs.getInt("questionType_ID"));
vo.setQuestionTypeName(rs.getString("questionType_Name"));
vo.setDifficultyDegreeID(rs.getInt("difficultyDegree_ID"));
vo.setDifficultyDegreeName(rs.getString("difficultyDegree_Name"));
vo.setExplains(rs.getString("explains"));
vo.setObjectName(rs.getString("object_Name"));
vo.setOptionA(rs.getString("option_A"));
vo.setOptionB(rs.getString("option_B"));
vo.setOptionC(rs.getString("option_C"));
vo.setOptionD(rs.getString("option_D"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps = null;
if(rs!=null) rs = null;
sql = null;
}
return vo;
}
public Object findByObject(Object obj) throws SQLException {
// TODO Auto-generated method stub
return null;
}
/**
* @author 石小军
* 得到所有试题的记录数
* @return int (记录数)
*/
public int getCount(Object obj) throws SQLException {
Integer integer = new Integer(0);
String sql = "";
StringBuffer sb = new StringBuffer("SELECT COUNT(*) FROM question WHERE 1=1");
PreparedStatement ps = null;
ResultSet rs = null;
try {
QuestionDTO dto = (QuestionDTO)obj;
if(dto.getQuestionID() != 0){
sb.append(" AND QUESTION_ID="+dto.getQuestionID()+" ");
}
if(dto.getQuestionName() != null&&!"".equals(dto.getQuestionName())){
sb.append(" AND QUESTION_NAME LIKE '%"+dto.getQuestionName()+"%'");
}
sql = sb.toString();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
integer = new Integer(rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps != null) ps.close();
if(rs != null) rs.close();
sql = null;
}
return integer;
}
public Object getMaxId(Object obj) throws SQLException {
// TODO Auto-generated method stub
return null;
}
/**
* @author 石小军
* 插入试题
* @return int (插入成功返回“1”,否则返回“0”)
*/
public Object insert(Object obj) throws SQLException {
Integer m = 0;
String sql = "";
QuestionDTO dto = null;
PreparedStatement ps = null;
try {
dto = (QuestionDTO)obj;
System.out.println(dto.getQuestionTypeID());
if(dto.getQuestionTypeID()==1||dto.getQuestionTypeID()==2){
sql = "INSERT INTO question(QUESTION_NAME,QUESTION_RESULT," +
"DIFFICULTYDEGREE_ID,QUESTIONTYPE_ID,OBJECT_NAME,EXPLAINS,OPTION_A,OPTION_B,OPTION_C,OPTION_D) " +
" VALUES(?,?,?,?,?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, dto.getQuestionName());
ps.setString(2, dto.getQuestionResult());
ps.setInt(3, dto.getDifficultyDegreeID());
ps.setInt(4, dto.getQuestionTypeID());
ps.setString(5, dto.getObjectName());
ps.setString(6, dto.getExplains());
ps.setString(7, dto.getOptionA());
ps.setString(8, dto.getOptionB());
ps.setString(9, dto.getOptionC());
ps.setString(10, dto.getOptionD());
ps.executeUpdate();
m = 1;
}else if(dto.getQuestionTypeID()==3||dto.getQuestionTypeID()==4||dto.getQuestionTypeID()==5){
sql = "INSERT INTO question(QUESTION_NAME,QUESTION_RESULT," +
"DIFFICULTYDEGREE_ID,QUESTIONTYPE_ID,OBJECT_NAME,EXPLAINS) " +
" VALUES(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, dto.getQuestionName());
ps.setString(2, dto.getQuestionResult());
ps.setInt(3, dto.getDifficultyDegreeID());
ps.setInt(4, dto.getQuestionTypeID());
ps.setString(5, dto.getObjectName());
ps.setString(6, dto.getExplains());
ps.executeUpdate();
m = 1;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps.close();
sql = null;
dto = null;
}
return m;
}
/**
* @author 石小军
* 修改试题
* @return int (修改成功返回“1”,修改返回“0”)
*/
public Object update(Object obj) throws SQLException {
Integer m = 0;
Integer n = -1;
String sql = "";
QuestionVO vo = null;
QuestionDTO dto = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
dto = (QuestionDTO)obj;
sql = "UPDATE question SET QUESTION_NAME=?,QUESTION_RESULT=?," +
"EXPLAINS=?,DIFFICULTYDEGREE_ID=?,QUESTIONTYPE_ID=?," +
"OBJECT_NAME=?,OPTION_A=?,OPTION_B=?,OPTION_C=?," +
"OPTION_D=? WHERE QUESTION_ID=?";
ps = conn.prepareStatement(sql);
ps.setString(1, dto.getQuestionName());
ps.setString(2, dto.getQuestionResult());
ps.setString(3, dto.getExplains());
ps.setInt(4, dto.getDifficultyDegreeID());
ps.setInt(5, dto.getQuestionTypeID());
ps.setString(6, dto.getObjectName());
ps.setString(7, dto.getOptionA());
ps.setString(8, dto.getOptionB());
ps.setString(9, dto.getOptionC());
ps.setString(10, dto.getOptionD());
ps.setInt(11, dto.getQuestionID());
n = ps.executeUpdate();
if(n!=-1){
m = 1;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps!=null) ps.close();
dto = null;
sql = null;
}
return m;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -