【问题标题】:wildcards in preparedstatement not executing准备好的语句中的通配符未执行
【发布时间】:2015-05-30 18:41:59
【问题描述】:

我试图让通配符在我的preparedstatements 中执行,但在运行时,即使在使用适当的方法后,它也会在结果集中返回空结果,只有当我搜索语句执行的特定关键字时。

我的html文件:

          <form action="Searchjob" method="get">
          Enter desired job title <input type="text" name="find">
          <input type="submit" name="submit2" value="submit">
          </form>

Servlet 文件:

        @WebServlet("/Searchjob")
        public class Searchjob extends HttpServlet {


@SuppressWarnings("static-access")
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();

   String r=(String)request.getParameter("find");


    List list3=null;
    Searchjob2 t2=new Searchjob2();

     try {
       list3=t2.Seek(r);
    }      catch (ClassNotFoundException ex) {

    }
   if(list3==null){ out.print(" <center><h1>No jobs found</center></h1>");}
   else{

    ArrayList<String> findr=new ArrayList<String>(list3);
    Iterator itt=findr.iterator();
    out.print("<html><body><center><h1>Jobs available</h1></center>");
    out.println("<center><table><tr><th>Job id</th><th>Jod title</th><th>Address</th><th>Branch</th><th>Technology</th><th>Description</th><th>Experience</th><th>Salary</th></tr>");
    while(itt.hasNext())
    {
        Ajob item=(Ajob)itt.next();
        out.print("<tr><th>"); 
        out.print(" "+item.geta()+" "); 
        out.print("</th>");
        out.print("<th>"); 
        out.print(" "+item.getjtit()+" "); 
        out.print("</th>");
        out.print("<th>");
        out.print(" "+item.getc()+" "); 
        out.print("</th>");
        out.print("<th>"); 
        out.print(" "+item.getd()+" "); 
        out.print("</th>");
        out.print("<th>"); 
        out.print(" "+item.gete()+" "); 
        out.print("</th>");
        out.print("<th>");
        out.print(" "+item.getf()+" "); 
        out.print("</th>");
        out.print("<th>");
        out.print(" "+item.getg()+" "); 
        out.print("</th>");
        out.print("<th>");
        out.print(" "+item.geth()+" ");           
        out.print("</th></tr>");


    }out.print("</center></table>");
}

}}

jdbc-odbc 文件:

        @WebServlet("/Searchjob2")
        public class Searchjob2 {

      public  List Seek(String r) throws ClassNotFoundException{
   { 


   try{
                 Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection(
        "jdbc:oracle:thin:@localhost:1521:xe", "system", "system");

         String forSql = "%" + r + "%";
         String query = "select * from Postjob where jtit like ?";
         PreparedStatement PrepStmt = con.prepareStatement(query);
         PrepStmt.setString(1, forSql);

        ResultSet rs = PrepStmt.executeQuery();

                 List seekr=new ArrayList();


                 while(rs.next())
                 {
                     String a=rs.getString("jid");
                     String b=rs.getString("jtit");
                     String c=rs.getString("addr"); 
                     String d=rs.getString("branch");    
                     String e=rs.getString("tech");    
                     String f=rs.getString("descp");
                     String g=rs.getString("exp"); 
                     String h=rs.getString("sal"); 
                     seekr.add(new Ajob(a,b,c,d,e,f,g,h));

                 }
    if(seekr.isEmpty()){
        return null;
    }else{
return seekr;
    }   
   }
   catch(SQLException e){

   }
    return null;
   }

   }
   }

在服务器上运行时,如果我在文本框中输入“manager”关键字,服务器会返回结果,但在输入“themanager”或“managerguru”等通配符时,它会返回空结果。 是通配符的语法问题还是其他问题?

【问题讨论】:

  • 您能否详细说明“但它们不起作用”?你有错误吗?错误的结果?
  • 你没有向我们展示完整的代码,r 是什么?
  • @Mureinik ,我在通配符上得到空结果集,r 也是从文本框接收的输入

标签: java mysql prepared-statement jdbc-odbc


【解决方案1】:

您可能需要将通配符放在单引号内。

【讨论】:

  • 是的,我尝试使用单引号,但在执行查询后,它仍然返回空结果集
【解决方案2】:

解决了伙计们。 我使用了以下语法:

        String query = "select * from Postjob where jtit like(?)"; 
        PreparedStatement PrepStmt = con.prepareStatement(query);
        PrepStmt.setString(1, "%r%");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2012-01-05
    • 2014-09-04
    相关资源
    最近更新 更多