📄 questiondao.java
字号:
package dao;
import java.sql.*;
import java.util.ArrayList;
import po.QuestionPO;
public class QuestionDAO
{
private Connection conn = null;
private Statement state = null;
private ResultSet rs = null;
//根据科目ID查出全部试题
public ArrayList findByCid(int cid)
{
ArrayList array = new ArrayList();
conn = Tools.getConnection();
try {
state = conn.createStatement();
rs = state.executeQuery("select * from question where cid="+cid);
while(rs.next())
{
QuestionPO qpo = new QuestionPO();
qpo.setCid(rs.getInt("cid"));
qpo.setAnswer(rs.getString("answer"));
qpo.setDid(rs.getInt("did"));
qpo.setQuestion(rs.getString("question"));
qpo.setOptionA(rs.getString("optionA"));
qpo.setOptionB(rs.getString("optionB"));
qpo.setOptionC(rs.getString("optionC"));
qpo.setOptionD(rs.getString("optionD"));
qpo.setQscore(rs.getInt("qscore"));
array.add(qpo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(rs != null)
rs.close();
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return array;
}
//根据问题ID查找
public QuestionPO findByQid(int qid)
{
QuestionPO qpo = null;
conn = Tools.getConnection();
try {
state = conn.createStatement();
rs = state.executeQuery("select * from question where qid="+qid);
while(rs.next())
{
qpo = new QuestionPO();
qpo.setCid(rs.getInt("cid"));
qpo.setAnswer(rs.getString("answer"));
qpo.setDid(rs.getInt("did"));
qpo.setQuestion(rs.getString("question"));
qpo.setOptionA(rs.getString("optionA"));
qpo.setOptionB(rs.getString("optionB"));
qpo.setOptionC(rs.getString("optionC"));
qpo.setOptionD(rs.getString("optionD"));
qpo.setQscore(rs.getInt("qscore"));
qpo.setChoose(rs.getString("choose"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(rs != null)
rs.close();
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return qpo;
}
//取问题ID
public int getNextQuestionID()
{
int myID = 0;
conn = Tools.getConnection();
try {
state = conn.createStatement();
rs = state.executeQuery("select max(qid) qid from question");
if(rs.next())
{
myID = rs.getInt("qid");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(rs != null)
rs.close();
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ++myID;
}
//添加问题记录
public boolean addQuestion(QuestionPO question)
{
boolean isok = false;
conn = Tools.getConnection();
try {
state = conn.createStatement();
int i = state.executeUpdate("insert into question values("+question.getQid()+","+question.getCid()+",'"+question.getQuestion()+"','"+question.getOptionA()+"','"+question.getOptionB()+"','"+question.getOptionC()+"','"+question.getOptionD()+"','"+question.getAnswer()+"',"+question.getDid()+","+question.getQscore()+")");
if(i > 0)
{
isok = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isok;
}
//修改问题记录
public boolean updateQuestion(QuestionPO question)
{
boolean isok = false;
conn = Tools.getConnection();
try {
state = conn.createStatement();
int i = state.executeUpdate("update question set cid="+question.getCid()+",question ='"+question.getQuestion()+"',optionA ='"+question.getOptionA()+"',optionB ='"+question.getOptionB()+"',optionC ='"+question.getOptionC()+"',optionD ='"+question.getOptionD()+"',answer ='"+question.getAnswer()+"',did ="+question.getDid()+",qscore ="+question.getQscore()+" where qid="+question.getQid());
if(i > 0)
{
isok = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isok;
}
//删除问题记录
public boolean deleteQuestion(int qid)
{
boolean isok = false;
conn = Tools.getConnection();
try {
state = conn.createStatement();
int i = state.executeUpdate("delete from question where qid="+qid);
if(i > 0)
{
isok = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(state != null)
state.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isok;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -