itemdao.java

来自「只做是单选题!数据库表有三张:EUser,EItem,Score」· Java 代码 · 共 121 行

JAVA
121
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package com.exam.model;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;/** * * @author Administrator */public class ItemDAO {    private static final String GET_EXAM = "SELECT * FROM EITEM ";    private String GET_ANSWER = "SELECT answer FROM EITEM WHERE  QID =? ";    private DataSource datasource;    public ItemDAO() {        try {            Context ctx = new InitialContext();            datasource = (DataSource) ctx.lookup("jdbc/examDB");        } catch (Exception e) {            e.printStackTrace();        }    }    public List getExam() {        List exam = new ArrayList();        Connection con = null;        PreparedStatement ps = null;        ResultSet rs = null;        try {            con = datasource.getConnection();            ps = con.prepareStatement(GET_EXAM);            rs = ps.executeQuery();            while (rs.next()) {                Item item = new Item(rs.getInt("QID"), rs.getString("question"), rs.getString("option_a"),                        rs.getString("option_b"), rs.getString("option_c"), rs.getString("option_d"), rs.getString("answer"));                exam.add(item);            }        } catch (SQLException se) {            se.printStackTrace();        } finally {            if (rs != null) {                try {                    rs.close();                } catch (Exception e) {                    e.printStackTrace();                }            }            if (ps != null) {                try {                    ps.close();                } catch (Exception e) {                    e.printStackTrace();                }            }            if (con != null) {                try {                    con.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }        return exam;    }    public String getAnswer(int QID) {        String answer = null;        Connection con = null;        PreparedStatement ps = null;        ResultSet rs = null;        try {            con = datasource.getConnection();            ps = con.prepareStatement(GET_ANSWER);            ps.setInt(1, QID);            rs = ps.executeQuery();            while (rs.next()) {                answer = rs.getString("answer");            }        } catch (SQLException se) {            se.printStackTrace();        } finally {            if (rs != null) {                try {                    rs.close();                } catch (Exception e) {                    e.printStackTrace();                }            }            if (ps != null) {                try {                    ps.close();                } catch (Exception e) {                    e.printStackTrace();                }            }            if (con != null) {                try {                    con.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }        return answer;    }}

⌨️ 快捷键说明

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