2015-01-20 36 views
0

我已经编写了一个程序来从java数据库中选择一个特定的列,并且我想使用jsp将其显示在我的服务器端。 在我的jsp页面,我有一个选择标记和选择会从我的数据库显示的选项,这个选项的值时,一些选项如何迭代jsp中的列表从mysql数据库获得的值

如:机场将显示这是我插在我的数据库 当唯一的机场的值我在服务器上调试我的代码,我可以看到这样的值[abc,xyz],但是当我在服务器上运行它时,我只能看到我选择的选项(机场),而不是值。

所以我想我需要遍历列表中,我做了如何遍历在JSP 2页列表中的一些研究,但我想它不是,应该怎样使用它

代码上面的方法:

java端,Java代码来选择从数据库(工作罚款)

public List readCategoryMsg(String gcm_msg_type) { 
    List msgList = new ArrayList<String>(); 
    try { 

     Class.forName("com.mysql.jdbc.Driver"); 
     connection = (Connection) 
     DriverManager.getConnection("jdbc:mysql://localhost:3306/gcm", 
     "root", "root"); 
     state = (Statement) connection.createStatement(); 

     prep = (PreparedStatement) connection 
       .prepareStatement("Select gcm_message from gcm_msg where 
     gcm_msg_type = ?"); 
     prep.setString(1, gcm_msg_type); 
     rSet = prep.executeQuery(); 
     while (rSet.next()) { 
      String msg_type = rSet.getString("gcm_message"); 
      msgList.add(msg_type); 

     } 

     connection.close(); 

    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
    } 
    return msgList; 
    } 

jsp页面1列:(其具有选项来选择,并将其提交给行动页面来显示输出)

 <form action="category_type_results.jsp" method="get"> 
     <div align="left"> 
      <br><select name="category_type"> 
       <option value="airport">Airport</option> 
       <option value="art gallery">Art Gallery</option> 
       <option value="atm">ATM</option> 
       <option value="bank">Bank</option> 
       <option value="book store">Book Store</option> 
       <option value="bus station">Bus Station</option> 
       <option value="cafe">Cafe</option> 
      </div> 
      <div> 
      <input type="submit" value="Show Category Message " /> 
      </div> 
    </form> 

JSP第2页:

我注释掉我尝试过的事情,但不正确的category_type_results.jsp,需要关于如何从我的数据库显示所选选项的值的列表一些帮助

<% 
ServiceSql serviceSql = new ServiceSql(); 
String gcm_msg_type = request.getParameter("category_type"); 
serviceSql.readCategoryMsg(gcm_msg_type); 
//List<String> msgList = serviceSql.readCategoryMsg("gcm_msg_type"); 
//for (int i = 0; i < msgList.size(); i++) { 
// msgList.get(i); 
} 
%> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
</head> 
<body> 

<h3> 
    <%=gcm_type_msg%> 
    //<%=msgList%> 

</h3> 

</body> 

怎么我的数据库列看,同一个列表我要显示在我的服务器端

gcm_message(column name) 
hello gcm (values) 
hello gcm .. 
hello gcm .. 
hello gcm .. 
hello gcm .. 
hello gcm (values) 

任何sugestions将是巨大的帮助的感谢您

+0

这是一个有点混乱。你指的是什么清单?如果你选择“机场”,那么你应该只获得一个价值。你还说:“...我的代码在服务器上,我可以看到像这样的值[abc,xyz],但是当我在服务器上运行它时......”。你使用了“服务器”两次。你能更清楚一点吗? – Ascalonian 2015-01-20 19:33:07

回答

0

使用JSTL通过列表进行迭代:

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
</head> 
<body> 
<% 
    ServiceSql serviceSql = new ServiceSql(); 
    String gcm_msg_type = request.getParameter("category_type"); 
    List msgList= serviceSql.readCategoryMsg(gcm_msg_type); 
    request.setAttribute("msgList", msgList); 
    %> 
<h3> 
    <c:forEach items="${msgList}" var="msg"> 
    ${msg}   
    </c:forEach> 
</h3> 
相关问题