【问题标题】:jsp page doesn't get the data from mysqljsp页面没有从mysql获取数据
【发布时间】:2014-12-10 19:05:33
【问题描述】:

我在使用 netbeans 中的 jsp 页面在 mysql 中查看我的数据库中的数据时遇到问题。 这是我在 jsp 页面中的代码:

<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.DatabaseMetaData"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.ArrayList" import="gettingItem.Item"%>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>   
        <h2>Home Page</h2>

     <form name="myform" onsubmit="return OnSubmitForm();" method="get">


             <select name="select"  size="12" style = "width: 150px">

<%
    String url = "jdbc:mysql://localhost:3306/itemsdb ";
    String user = "admin";
    String password = "admin";
    String Line;
    Connection Con = null;
    Statement Stmt = null;

    ResultSet RS = null;
    try
    {
        Class.forName("com.mysql.jdbc.driver");
        Con = DriverManager.getConnection(url, user, password);
        Stmt =  Con.createStatement();
        Stmt.executeQuery("SELECT ItemName FROM items");
        RS = Stmt.getResultSet();

        while(RS.next()){%>

                <Option value="<%=RS.getString("ItemName")%>">
                    <%=RS.getString("ItemName")%>
                </Option>
                </select>

             <input type="submit" value="Remove" onclick="document.pressed = this.value">
            <input type="submit" value="Display" onclick="document.pressed = this.value">
          </form>
                <form action="Add_Item.jsp" method ="get" >
            <input type="submit" value="Add">
        </form>

        <%

        }
        RS.close();
  Stmt.close();
  Con.close();
    }
    catch (Exception cnfe){
        System.err.println("Exception: "+cnfe);
    }

%>


</body>
</html>

我想在 jsp 页面的下拉列表中查看数据,但它似乎不起作用,我不知道我的代码有什么问题?

【问题讨论】:

    标签: html mysql jsp netbeans


    【解决方案1】:

    它是 MVC 应用程序吗?我的意思是,最好将数据加载到 dao 中,通过控制器将它们返回到视图中。这样你就可以调试出了什么问题。
    我是这样做的: 道

    public List<String> getCiid(String eingabe)
    {
    
        String tmp = new String("SELECT inst_ciid FROM T_INSTANCE WHERE inst_ciid like '%"+eingabe.toUpperCase()+"%'");
        Query query = this.getSession().createSQLQuery(tmp);
    
        List liste = query.list();
    
        return liste;
    }
    

    控制器:

    @RequestMapping(value = "/getCiid", method = RequestMethod.GET)
    public void getCiid(HttpServletResponse response,@RequestParam Map<String, String> params) 
    {
        String jsonResponse = null;
    
        List<String> liste = instanzDao.getCiid(params.get("term"));  
        jsonResponse = new Gson().toJson(liste);
        response.setContentType("application/json");
        try 
        {
            response.getOutputStream().print(jsonResponse);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    

    查看:

    $( "#ciid" ).autocomplete({
          source: "getCiid",
          minLength: 3,
          select: function( event, ui ) {
    
           // whatever you want
          }
    }); 
    <td>
                        <label for="ciid"> CIID: </label>
                        <form:input path="instanceCiid" id="ciid" />
                    </td>
    

    【讨论】:

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