简体   繁体   English

试图从 mySQL 数据库中获取结果并将其显示在 jsp 文件中

[英]Trying to fetch results from mySQL database and display it in a jsp file

using a MVC model to try and fetch from a mySQL db.使用 MVC model 尝试从 mySQL 数据库中获取。 The code tries to query db and put the results into and array to pass it to the controller. The jsp page can then call it.该代码尝试查询db并将结果放入and数组以将其传递给controller。然后jsp页面可以调用它。 At the moment, I am not getting results to display even though it can compile目前,即使它可以编译,我也没有显示结果

View看法

public class ViewQA {
    public static void main(String category, String questions, String answers) {
        // TODO Auto-generated constructor stub
    }

    public static List<ViewQA> listData() throws SQLException, ClassNotFoundException {
        List<ViewQA> listData = new ArrayList<>();

        try (java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/examgenappschema",
                "EXadmin", "abc123");) {
            
        PreparedStatement select = null;
        String queryString = ("SELECT category, questions, answers FROM examgen");
        
        select = conn.prepareStatement(queryString);
        
        
            ResultSet rs = select.getResultSet();
            while (rs.next()) {
                rs.getString("category");
                rs.getString("questions");
                rs.getString("answers");
                ViewQA results = new ViewQA();
                listData.add(results);
            }
        }
        return listData;
    }
}

Controller Controller

    @Controller
public class ExamGenController {
    @RequestMapping(value = "/examgen", method = RequestMethod.GET)
    public String DataArray(@ModelAttribute("listData") ViewQA listData, ModelMap model)
            throws ClassNotFoundException, SQLException {
        return "examgen";
    }}

jsp page jsp 页

    <!DOCTYPE html>
<html>
    <body>
    <center>
        <h1>List of Questions and Answers</h1>
    </center>
    <div align="center">
        <table border="1" cellpadding="3">
            <tr>
                <th>category</th>
                <th>questions</th>
                <th>answers</th>
            </tr><c:forEach var="listData" items="${DataArray}"> 
        <div class="grid-item4"> ${DataArray.category} </div>
        <div class="grid-item4"> ${DataArray.questions} </div>
        <div class="grid-item4"> ${DataArray.answers} </div>
</c:forEach>        
        </table>
    </div>   
</body>
</html>

You forgot to add your query result to ViewQA .您忘记将查询结果添加到ViewQA You adding a blank ViewQA object into the list.您将空白 ViewQA object 添加到列表中。

You are taking var but not using it in the code try this way.您正在使用 var 但未在代码中使用它,请尝试这种方式。

<c:forEach var="listData" items="${DataArray}"> 
        <div class="grid-item4"> ${listData.category} </div>
        <div class="grid-item4"> ${listData.questions} </div>
        <div class="grid-item4"> ${listData.answers} </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM