从SQL获取数据并放入jquery

时间:2015-07-24 10:04:27

标签: jquery jsp autocompletebox

我正在尝试在jsp上执行自动完成文本框。但我不知道如何包含从数据库中检索到的所有数据,以匹配用户键入的值。请帮助,非常感谢。

  <%
    String FRUIT= "";

    TEST.makeConnection();
    String SQL = "SELECT * FROM TB_FRUIT WITH UR";
    TEST.executeQuery(SQL);     
    while(TEST.getNextQuery())
    {
        FRUIT= TEST.getColumnString("FRUIT_DESCP");
    }   
    TEST.takeDown();    
   %>

  <html>
  <head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
   <script>
    $(function() {
    var availableTags = [
      //how can i include the data(FRUIT) from database to put in here? 

      "Apple", // i hardcode this to do testing for my current autocomplete textbox
      "Avocado", // i hardcode this to do testing for my current autocomplete textbox
      "Banana",// i hardcode this to do testing for my current autocomplete textbox
      "Durian",// i hardcode this to do testing for my current autocomplete textbox
      "Mango",// i hardcode this to do testing for my current autocomplete textbox
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
      });
    });
  </script>      
  <body>
       <div class="ui-widget">
         <label for="tags">Tags: </label>
         <input id="tags">
       </div>
  </body>
  </html>

1 个答案:

答案 0 :(得分:1)

我不是JSP专家,但我认为我可以在您的代码中发现一些问题:

  1. FRUIT将始终具有查询中最后一条记录的值,因为您没有连接值,而是将值替换为新值。

    你希望FRUIT成为这样的字符串列表:"fruit1","fruit2","fruit3",...,要做到这一点,你应该这样做:

    FRUIT += "\"" + TEST.getColumnString("FRUIT_DESCP") + "\",";
    
  2. 即使您正确获得了值,也不会在网站上显示它。既然您拥有自动填充可以理解的格式的FRUIT值,您只需要打印它:

    var availableTags = [
        <% out.print(FRUIT); %>
    ];
    
  3. 这应该可以解决问题。

    最后,代码看起来像这样:

    <%
        String FRUIT= "";
    
        TEST.makeConnection();
        String SQL = "SELECT * FROM TB_FRUIT WITH UR";
        TEST.executeQuery(SQL);     
        while(TEST.getNextQuery())
        {
            FRUIT += "\"" + TEST.getColumnString("FRUIT_DESCP") + "\",";
        }   
        TEST.takeDown();    
       %>
      <html>
      <head>
      <meta charset="utf-8">
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">
       <script>
        $(function() {
        var availableTags = [
            <% out.print(FRUIT); %>
        ];
        $( "#tags" ).autocomplete({
            source: availableTags
          });
        });
      </script>      
      <body>
           <div class="ui-widget">
             <label for="tags">Tags: </label>
             <input id="tags">
           </div>
      </body>
    </html>