在servlet中搜索mysql的代码?

时间:2015-02-18 13:33:13

标签: mysql servlets

什么是在servlet中搜索mysql的代码?

我需要简单的编码,任何人请帮助我

我使用Html创建了一个注册页面(名称,资格,国家,州),我在mysql中创建了一个注册表,

然后我创建了servlet页面以获取从user到db的值,这些值已成功插入到mysql表中..

然后我在我的主页上给了一个搜索框。

现在我想通过搜索选项

获取表值

例如:使用“name”我想从mysql中搜索值..

我需要在servlet中使用简单的搜索代码,不要粘贴更多编码,我在软件开发中更新..

1 个答案:

答案 0 :(得分:3)

我创建了一个简单的演示项目,你提到我使用jstl:core库,可以从here下载

<强> 的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID"
        version="3.1">
        <display-name>TestServlet</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>MyController</servlet-name>
            <servlet-class>controller.MyController</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyController</servlet-name>
            <url-pattern>/MyController</url-pattern>
        </servlet-mapping>

</web-app>

index.jsp

 <%@ page
        language="java"
        contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib
        uri="http://java.sun.com/jsp/jstl/core"
        prefix="c"%>
    <html>
    <head>
    <meta
        http-equiv="Content-Type"
        content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
<form
        action="MyController"
        method="post">
        <input
            type="text"
            name="name"
            value="yourUserName"> <input
            type="submit"
            value="Search">
    </form>
        <table title="Users List">
            <c:forEach
                items="${users}"
                var="user">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

<强> MyController.java

package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.User;

public class MyController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public MyController() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        RequestDispatcher rd = null;
        rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Get name request parameter
        String name = request.getParameter("name");

        // User user1 = new User();
        // User user2 = new User();
        // user1.setId(0);
        // user1.setName("test1");
        // user2.setId(0);
        // user2.setName("test2");

        List<User> users = new ArrayList<User>();

        // users.add(user1);
        // users.add(user2);



         //DB Operation query your users
         users=query(name);

        request.setAttribute("users", users);

        RequestDispatcher rd = null;
        rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }
}

因此应从服务层调用MyController#query方法,并根据您的条件进行查询(在演示名称中或可以是任何内容)

编辑: 请确保user获取了属性方法,例如此示例中的getIdgetName