【问题标题】:trying to get a query to use the LIKE function with a variable in JSP试图获取查询以在 JSP 中将 LIKE 函数与变量一起使用
【发布时间】:2011-01-13 13:56:07
【问题描述】:

我有一个查询用于提取用户名和有关用户的信息。在 Access 中,我有 LIKE 功能,因此用户不必输入特定名称。我现在将它转移到 JSP。这是我在 JSP 中遇到问题的查询行:

WHERE ObjectName Like '" + "%"+ VariableName + "%" +"';

查询运行良好,但即使我输入了整个名称,也不会显示任何信息。如果我将其更改为:

WHERE ObjectName = '" + VariableName +"';

它有效,但我想让用户有机会输入部分名称,以防他们不知道如何拼写名称或输入错误。任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: java jsp jdbc


    【解决方案1】:

    您显示的行有点奇怪,但在语法上是有效的。所以问题出在其他地方。 variableName 究竟包含什么?

    也就是说,您不应该在 JSP 文件中编写原始 Java 代码。在 Java 类中执行此操作。您可以使用Servlet 类来预处理或后处理请求。还要抓住PreparedStatement 以避免SQL injections。这是一个启动示例:

    public List<User> search(String username) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<User> users = new ArrayList<User>();
    
        try {
            connection = database.getConnection();
            statement = connection.prepareStatement("SELECT id, username, age, email FROM user WHERE username LIKE ?");
            statement.setString(1, "%" + username + "%");
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                users.add(mapUser(resultSet));
            }
        } finally {
            close(connection, statement, resultSet);
        }
    
        return users;
    }
    

    【讨论】:

      【解决方案2】:
      1. 避免在 JSP 中编写 SQL 查询
      2. "SELECT * FROM something WHERE ObectName LIKE '%" + VariableName + "%'" 应该可以工作

      【讨论】:

        【解决方案3】:

        这是给初学者的答案 我创建了一个名为 ASHRAF 的数据库,然后我创建了一个名为 CASH 的表。代码如下

        CREATE TABLE CASH(NO INT NOT NULL PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(50) NOT NULL,ADDRESS VARCHAR(100),PET_NAME VARCHAR(50),PLACE VARCHAR(50),TYPE VARCHAR(20),TYPE_OF_PAY VARCHAR(20),AMOUNT INT(6) NOT NULL);
        

        这里的 NO 是自动增量,它是主键无论如何你可以使用我在下面给出的 jsp 代码从表中搜索内容

        我在这里使用 NAME ADDRESS 进行搜索,您可以使用 html 页面和 servlet 传递参数

        我创建的 html 页面(show.html)如下所示

        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>show.html</title>
        </head>
        <body>
        <h1><b><font color=020202>SHOW</font></b></h1><br><br>
           <form name="f6" action="getshow" method="POST" onsubmit="return check(this)">
           <table border="0">
           <tr>
          <td>Name :</td><td><input type="text" name="name"></td>
           </tr>
           <tr>
          <td>House Name :</td><td><input type="text" name="address"></td>
           </tr>
           <tr>
           <td><br><input type="SUBMIT" value="submit"></td>
           </tr>
           </table>
           </form>
        </body>
        </html> 
        

        下面给出的servlet是(getshow.java)

        package Servlets;
        
        
        
        import java.io.IOException;
        
        
        
        import javax.servlet.RequestDispatcher;
        
        
        
        import javax.servlet.ServletException;
        
        
        
        import javax.servlet.http.HttpServlet;
        
        
        
        import javax.servlet.http.HttpServletRequest;
        
        
        
        import javax.servlet.http.HttpServletResponse;
        
        
        
        /**
        
        
        
         * Servlet implementation class getdata
        
        
        
         */
        
        
        
        public class getshow extends HttpServlet {
        
        
            private static final long serialVersionUID = 1L;
        
        
            /**
        
             * @see HttpServlet#HttpServlet()
        
             */
        
            public getshow() {
        
                super();
        
                // TODO Auto-generated constructor stub
        
            }
        
        
            /**
        
             * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        
             */
        
            protected void doGet(HttpServletRequest request, HttpServletResponse response)
        `` throws ServletException, IOException {
        
        
                // TODO Auto-generated method stub
        
            }
        
        
            /**`
        
             * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
        ``response)
        
        
             */
        
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
                // TODO Auto-generated method stub
        
                try{
        
                String url=null;
        
                String s1=request.getParameter("name");
        
                String s2=request.getParameter("address");
        
        
                request.setAttribute("name",s1);
        
        
        
                request.setAttribute("address",s2);
        
        
                    url="show.jsp";
        
                RequestDispatcher view=request.getRequestDispatcher(url);
        
                view.forward(request, response);
        
            } catch (Exception e) {
        
                // TODO Auto-generated catch block
        
                e.printStackTrace();
        
            } 
        
            }
        
        
        }
        

        下面给出的jsp文件是(show.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">
        
        
        
        <html>
        
        
        
        <head>`
        
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        
        
        <title>show.jsp</title>
        
        
        
        </head>
        
        
        
        <body>
        
        
        
        <%String aid=(String)request.getAttribute("name"); %>
        
        
        
        <%String sid=(String)request.getAttribute("address"); %>
        
        
        
        
        
          <%
        
        
        
        
                Connection con=null;
        
                ResultSet rs=null;
        
                String records=null;
        
                StringBuffer appender=new StringBuffer();
        
                java.sql.PreparedStatement st=null;
        
                try {
        
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
        
                    con=DriverManager.getConnection("jdbc:mysql://localhost/ASHRAF?user=root&password=password");
        
                    st=con.prepareStatement("select *from CASH where NAME like '" + aid + "%" +"' and ADDRESS like '" + sid + "%" +"'");
        
                    rs=st.executeQuery();
        
        
                    %>
        
            <center><TABLE cellpadding="15" border="2">
        
            <TR>
        
        
        <TH>NO</TH>
        
        
        
        <TH>NAME</TH>
        
        
        
        <TH>HOUSE NAME</TH>
        
        
        
        <TH>PET NAME</TH>
        
        
        
        <TH>PLACE</TH>
        
        
        
        <TH>TYPE OF OCCATION</TH>
        
        
        
        <TH>TYPE OF PAY</TH>
        
        
        
        <TH>AMOUNT</TH>
        
        
        
        </TR>
        
        
        
         <%
        
        
        
        while (rs.next()) {
        
        
        
        %>
        
        
        
        <TR>
        
        
        
        <TD><%=rs.getString(1)%></TD>
        
        
        
        <TD><%=rs.getString(2)%></TD>
        
        
        
        <TD><%=rs.getString(3)%></TD>
        
        
        
        <TD><%=rs.getString(4)%></TD>
        
        
        
        <TD><%=rs.getString(5)%></TD>
        
        
        
        <TD><%=rs.getString(6)%></TD>
        
        
        
        <TD><%=rs.getString(7)%></TD>
        
        
        
        <TD><%=rs.getString(8)%></TD>
        
        
        
        </TR>
        
        
        
        <% } %>
        
        
        
        </TABLE>
        
        
        
        </center>
        
        
        
        </div>
        
        
        
        <%
        
        
            } catch (Exception e) {
        
            // TODO Auto-generated catch block
        
            e.printStackTrace();
        
        
        } 
        
        
        
        finally
        
        
        
        {
        
        
            try {
        
                con.close();
        
            } catch (SQLException e) {
        
                // TODO Auto-generated catch block
        
                e.printStackTrace();
        
            }
        
        
        } %>
        
        
        
        </body>
        
        
        
        </html>
        

        现在您可以使用名称或地址或两者都进行搜索。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-01
          • 2012-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多