我正在从数据库中检索数据并以JSP的形式显示在表中,但是我不知道如何在文本字段上显示它。

例如

当我搜索索引号时为

  • 结果(名称,地址,年龄)必须进入我的JSP
  • 中的文本字段

    我的代码:
    public class S2 extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "shoppingCart";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "";
    
        Statement st;
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected!");
            String pid = request.getParameter("pid");
    
            ArrayList al = null;
            ArrayList pid_list = new ArrayList();
            String query = "select * from user where uid='" + pid + "' ";
    
            System.out.println("query " + query);
            st = conn.createStatement();
            ResultSet rs = st.executeQuery(query);
    
            while (rs.next()) {
    
                al = new ArrayList();
    
                out.println(rs.getString(1));
                out.println(rs.getString(2));
                out.println(rs.getString(3));
                out.println(rs.getString(4));
                out.println(rs.getString(5));
    
    
                al.add(rs.getString(1));
                al.add(rs.getString(2));
                al.add(rs.getString(3));
                al.add(rs.getString(4));
                al.add(rs.getString(5));
    
    
                System.out.println("al :: " + al);
                pid_list.add(al);
            }
    
    
            request.setAttribute("piList", pid_list);
            RequestDispatcher view = request.getRequestDispatcher("/searchview.jsp");
            view.forward(request, response);
            conn.close();
            System.out.println("Disconnected!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    

    最佳答案

    确保在项目中包含了jdbc驱动程序,并对其进行“构建”。然后:

  • 建立数据库连接并检索查询结果。
  • 返回查询结果并保存在ResultSet对象中。
  • 遍历对象并显示查询结果。

  • 下面的示例代码详细说明了这一点。
    String label = request.getParameter("label");
    //retrieving a variable from a previous page
    
    Connection dbc = null; //Make connection to the database
    Class.forName("com.mysql.jdbc.Driver");
    dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/works", "root", "root");
    
    if (dbc != null)
    {
        System.out.println("Connection successful");
    }
    ResultSet rs = listresult.dbresult.func(dbc, label);
    //The above function is mentioned in the end.
    //It is defined in another package- listresult
    
    while (rs.next())
    {
    %>
    <form name="demo form" method="post">
        <table>
            <tr>
                <td>
                    Label Name:
                </td>
                <td>
                    <input type="text" name="label"
                    value="<%=rs.getString("lname")%>">
                </td>
            </tr>
        </table>
    </form>
    <% } %>
    
    
    public static ResultSet func(Connection dbc, String x)
    {
        ResultSet rs = null;
        String sql;
        PreparedStatement pst;
        try
        {
            sql = "select lname from demo where label like '" + x + "'";
            pst = dbc.prepareStatement(sql);
            rs = pst.executeQuery();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            String sqlMessage = e.getMessage();
        }
        return rs;
    }
    

    我试图使这个例子尽可能详细。如果有任何疑问,请询问。

    10-07 12:58
    查看更多