⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dquestion.java

📁 在线模拟选课系统
💻 JAVA
字号:
/*
 * This product includes software developed by the
 * Apache Software Foundation (http://www.apache.org/).
 */
package ch07.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

import javax.servlet.http.HttpSession;

import ch07.*;
import ch07.object.unit.*;

/**
 * 针对问题信息的数据处理类
 * @author ShenYK
 * @version 1.0
 */
public class DQuestion extends DCommon
{
    //随机获得该分类指定数目的题目
    public Vector getRandomQuestions( String sUsername, String sCategoryId, int iNumber )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Vector vQuestions = new Vector();
            stmt = conn.createStatement();
            //执行SQL语句,找出所有可用的题目ID
            String sQuery = "select * from question where category_id='" + sCategoryId 
                          + "' order by rand() limit 0," + iNumber;
            rs = stmt.executeQuery( sQuery );
            while ( rs.next() )
            {
                Question objQuestion = new Question(rs);
                vQuestions.add( objQuestion );
            }
            return vQuestions;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
    
    //获得指定分类的题目
    public Vector getAllQuestionsByCategoryId(  String sCategoryId )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            Vector vQuestions = new Vector();
            stmt = conn.createStatement();
            //执行SQL语句,找出所有可用的题目ID
            String sQuery = "select * from question where category_id='" + sCategoryId 
                          + "' order by question_id";
            rs = stmt.executeQuery( sQuery );
            while ( rs.next() )
            {
                Question objQuestion = new Question(rs);
                vQuestions.add( objQuestion );
            }
            return vQuestions;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
    
    //删除指定的题目
    public void deleteQuestionById(  String sQuestionId )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        
        try
        {
            stmt = conn.createStatement();
            //执行SQL语句,删除ID
            String sUpdateQuery = "delete from question where question_id='" + sQuestionId + "'";
            stmt.executeUpdate( sUpdateQuery );
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
    
    //获得指定ID的题目详细信息
    public Question getQuestionById(  String sQuestionId )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        ResultSet rs = null;
        
        try
        {
            stmt = conn.createStatement();
            //执行SQL语句,找出所有可用的题目ID
            String sQuery = "select * from question where question_id='" + sQuestionId + "'";
            rs = stmt.executeQuery( sQuery );
            if ( rs.next() )
            {
                Question objQuestion = new Question(rs);
                return objQuestion;
            }
            return null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
    
    //修改指定的题目
    public void modifyQuestion ( Question questionObj )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        
        try
        {
            stmt = conn.createStatement();
            //执行SQL语句,修改题目
            String sUpdateQuery = "update question set "
                                + "subject = '" + questionObj.getSubject() + "', "
                                + "choice_a = '" + questionObj.getChoiceA() + "', "
                                + "choice_b = '" + questionObj.getChoiceB() + "', "
                                + "choice_c = '" + questionObj.getChoiceC() + "', "
                                + "choice_d = '" + questionObj.getChoiceD() + "', "
                                + "answer = '" + questionObj.getAnswer() + "' "
                                + "where question_id = '" + questionObj.getQuestionId() + "'";
            stmt.executeUpdate( sUpdateQuery );
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
    
    //添加一个新试题
    public void addQuestion ( Question questionObj )
        throws Exception
    {
        //获得数据库连接
        Connection conn = this.getDBConnection();
        if ( conn == null )
        {
            throw new Exception("数据库连接获得失败!");
        }
        Statement stmt = null;
        ResultSet rs = null;
        try
        {
            stmt = conn.createStatement();
            
            //执行SQL语句生成最大的recordId
            String sQuestionId = "";
            String sQuery = "select max(question_id) from question";
            rs = stmt.executeQuery( sQuery );
            rs.next();
            String sCurrentMaxId = rs.getString(1);
            //当前是第一次登录
            if ( sCurrentMaxId == null )
            {
                sQuestionId = "0000000001";
            }
            else
            {
                int iMaxCd = Integer.parseInt(sCurrentMaxId);
                sQuestionId = String.valueOf(iMaxCd+1);
                int iLength = sQuestionId.length();
                for(int i=10; i>iLength; i--)
                {
                    sQuestionId = "0" + sQuestionId;
                }
            }
            //执行SQL语句,修改题目
            String sUpdateQuery = "insert into question set "
                                + "question_id = '" + sQuestionId + "', "
                                + "category_id = '" + questionObj.getCategoryId() + "', "
                                + "difficulty = '" + questionObj.getDifficulty() + "', "
                                + "subject = '" + questionObj.getSubject() + "', "
                                + "choice_a = '" + questionObj.getChoiceA() + "', "
                                + "choice_b = '" + questionObj.getChoiceB() + "', "
                                + "choice_c = '" + questionObj.getChoiceC() + "', "
                                + "choice_d = '" + questionObj.getChoiceD() + "', "
                                + "answer = '" + questionObj.getAnswer() + "'";
            stmt.executeUpdate( sUpdateQuery );
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                rs.close();
                stmt.close();
                conn.close();
            }catch(Exception ex)
            {
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -