0

I tried to search the data from mysql databases but I got this error:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

main.jsp:

<form action = "SearchCheck" method="post">      
    <input type="text" id="search" class="fadeIn fourth" name="search" placeholder="search">
    <input type="submit" class="fadeIn fourth" value="Search">
</form>

SearchCheck.java:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    request.setCharacterEncoding("EUC-KR");     
    response.setContentType("text/html; charset=euc-kr");
    userSearch=request.getParameter("search");

    if(userSearch == null || userSearch =="" ) {

        PrintWriter out = response.getWriter();
        out.println("<script type=\"text/javascript\">");
        out.println("alert('이름을 채워주십시오');");
        out.println("location='main.jsp';");
        out.println("</script>");

    }


    else {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/member", "root", "Wlsdud1964");                         
            String sql = "SELECT * FROM  user where userName LIKE '%"+userSearch+"%'";              
            System.out.println(sql);        
            ps = conn.prepareStatement(sql);            
            ps.setString(1, userName);
            resultSet = ps.executeQuery();

            //response.sendRedirect("loginCheckResult.jsp");

            while(resultSet.next()) {               
                resultSet.getString(userName);
                resultSet.getString(userGroup);
                PrintWriter out = response.getWriter();
                out.println(resultSet.getString(userName) + resultSet.getString(userGroup));
                out.println("<br /");       
            }           
        } catch(Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(stmt != null)stmt.close();
                if(conn != null)conn.close();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }   
    }//else     
} //doPost
1

2 Answers 2

1

Tahir was almost right in his answer, the proper SQL query should be

String sql = "SELECT * FROM  user where userName LIKE ?";

and then you have to add % wildcards to the parameter "manually", i.e.

String queryString = "%" + userSearch + "%";
ps.setString(1, queryString);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, never noticed that. Will give it a look. Thanks for the info
0

EDIT: As Jozef mentioned in one answer, ? should not be put between string literal '%?%' so append % to the string parameter before it is set in the query There is no parameter in your sql query...

replace the query to

String sql = "SELECT * FROM  user where userName LIKE ?"

? is the parameter where your string will be set.
where string will be like "%"+inputString+"%";

3 Comments

i replaced the query to String sql = "SELECT * FROM user where userName LIKE '%?%'" but same error
it can't be. I think you check again or if it still persists, then someone wrote a comment on your question. Check that links 2nd answer
@TahirHussainMir you cannot put ? into a string literal ('%?%'), it has always to be a separate parameter (... WHERE userName LIKE ? ...), then you have to add the % wildcards to the value "manually". See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.