如何通过字符串键显示hashmap的内容?

时间:2014-12-11 15:00:20

标签: jsp servlets

我已经尝试过.key和.value但是我没有得到我想要的东西,我想显示hashmap的内容但仅通过密钥。这里是来源: 比恩:

public class LessonTimetable实现Serializable {

private Connection connection = null;
private ResultSet rs = null;
private PreparedStatement st = null;
private Map lessons = new HashMap<String, List<Lesson>>();
private DataSource ds = null;
public Lesson less;


public LessonTimetable() {
    // You don't need to make any changes to the try/catch code below
    try {
        // Obtain our environment naming context
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        // Look up our data source
        ds = (DataSource) envCtx.lookup("jdbc/LessonDatabase");//change to LessonDatabase..will also have to setup credentials for my virtualmin server account.
    } catch (Exception e) {
        System.out.println("Exception message is " + e.getMessage());
    }
    try {
        // Connect to the database - you can use this connection to 
        // create and prepare statements, and you don't need to worry about closing it
        //String username = "root";
        //String Password = "";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");//test locally first
        try {
            if (connection != null) {
                // TODO instantiate and populate the 'lessons' HashMap by selecting the relevant infromation from the database
                List<String> putDescriptions = new ArrayList<String>();
                List<String> putDates = new ArrayList<String>();
                List<String> putStartTime = new ArrayList<String>();
                List<Integer> Level = new ArrayList<Integer>();
                List<String> LessonID = new ArrayList<String>();
                List<String> endTime = new ArrayList<String>();
                String query = String.format("SELECT description,level,startDateTime,endDateTime,lessonid FROM LESSONS");
                st = connection.prepareStatement(query);
                rs = st.executeQuery();
                connection.setAutoCommit(false);
                st.setFetchSize(0);
                while (rs.next()) {
                    String getDescription = rs.getString("description");
                    int level = rs.getInt("level");
                    Timestamp startDate = rs.getTimestamp("startDateTime");
                    Timestamp endDate = rs.getTimestamp("endDateTime");
                    String LessonId = rs.getString("lessonid");
                    this.less = new Lesson(getDescription, startDate, endDate, level, LessonId);
                    putDescriptions.add(less.description);
                    putDates.add(less.date);
                    putStartTime.add(less.startTime);
                    Level.add(less.level);
                    LessonID.add(less.ID);
                    endTime.add(less.endTime);
                    this.lessons.put("description", putDescriptions);
                    this.lessons.put("StartDate", putDates);
                    this.lessons.put("StartTime", putStartTime);
                    this.lessons.put("EndTime", endTime);
                    this.lessons.put("Level",Level);
                    this.lessons.put("LessonID", LessonID);
                }
                rs.close();
                st.close();
            }
        } catch (Exception e) {
            System.out.println("Exception is ;" + e + ": message is " + e.getMessage());
        }
    } catch (Exception e) {
        System.out.println("Exception is ;" + e + ": message is " + e.getMessage());
    }
}

/**
 * @param ItemID
 * @return the items
 */
public Lesson getLesson(String ItemID) {
    return (Lesson) this.lessons.get(ItemID);
}

public Map getLessons() {
    return this.lessons;
}

}

  session.setAttribute("AvailableLessons", availableLessons.getLessons());
                   <c:forEach var="hash" items="${sessionScope.AvailableLessons}">
                    <tbody>
                        <tr>
                            <form action="" method="POST">
                                <td>
                                    <c:out value="${hash.key['description']}"/> throws a  PropertyNotFoundException
                                </td>

0 个答案:

没有答案