显示来自jspx中数据库的信息(使用jdbc)

时间:2011-12-05 18:05:33

标签: java mysql jsp jspx

我创建了一个要使用的bean,它返回一个包含主题的哈希映射

public class ModListBean {
    String connectionURL = "jdbc:mysql://localhost/cs_subjects";

public ModListBean() {

    try {

        // Load the database driver
        Class.forName("com.mysql.jdbc.Driver");

        // Get a Connection to the database
        connection = DriverManager.getConnection(connectionURL, "root", "");

        }catch(Exception e){

            System.out.println("Exception is ;"+e + ": message is " + e.getMessage());
        }


}

public Map getSubjects() {
    Map subjects = new HashMap<String, Subject>(); 
    String query = "SELECT * FROM cs_subject";

     try {
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {

                String dbId = rs.getString("id");
                String dbName = rs.getString("name");
                int dbCredits = rs.getInt("credits");

                Subject x = new Subject (dbId, dbName, dbCredits);
                subjects.put(rs.getString(1), x);

            } //end while

    }catch(SQLException e) {

        System.out.println("Exception is ;"+e + ": message is " + e.getMessage());
        return null;
    }
    return modules;

}

然后我尝试在jspx页面的表格中显示hashmap

<jsp:useBean class="model.ModListBean" id="modBean" scope="page"/>
        <table border="1">
            <tr>
                <th>Subject ID</th><th>Name</th><th>Mark</th>
            </tr>
            <c:forEach var="entry" items="${modBean.subjects}">
                    <tr>
                        <td><c:out value="${entry.key}"/></td>
                        <td><c:out value="${entry.value.name}"/></td>
                        <td><c:out value="${entry.value.credits}"/></td>                        
                    </tr>                    
            </c:forEach>
        </table>

当页面加载时,我得到表头但是里面没有数据。我也没有例外。谁能指出我哪里出错?

0 个答案:

没有答案