JSP中的结果集

我需要一些帮助,将结果集(rs)转发给jsp。 我已经在JAVA中实现了MVC结构(注意:我是新来的)。 同样的逻辑流程如下:

  • 基本形式:用户输入他的选择。
  • 在提交时,流被定向到一个servlet。
  • 流从servlet流转到Java文件,在该文件中处理数据库检索和其他逻辑。
  • 然后将结果发送回servlet。
  • Servlet将结果转发给JSP进行显示。
  • Servlet:

    package com.example.web;
    import com.example.model.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    public class CoffeeSelect extends HttpServlet {
    
      public void doPost( HttpServletRequest request, 
                          HttpServletResponse response) 
                          throws IOException, ServletException {
                String c = request.getParameter("type");
                CoffeeExpert ce = new CoffeeExpert();
                List result = ce.getTypes(c);
                request.setAttribute("styles", result);
                RequestDispatcher view = request.getRequestDispatcher("result.jsp");
                view.forward(request, response); 
              }
            }
    

    java文件:

        package com.example.model;
    
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.util.*;
    
        public class CoffeeExpert {
           public List<Types> getTypes(String test) {
    
              ResultSet rs = null;
             List<Types> list = new ArrayList();
             String Name = "na";
             String PCANo = "NotFound";
             String IP = "NotFound";
             Types type=new Types(); 
             if (test.equals("ABC")) {
             try{
             Connection con = getConnection();
             String Query = "select * from Table1";
             // System.out.println(Query);
    
              rs = con.createStatement().executeQuery(Query);
    
                     while (rs.next()) {
                         type.setName(rs.getString(1));
                         type.setPCANo(rs.getString(2));
                         type.setIP(rs.getString(3));
                       System.out.println(Name+"  "+PCANo+"  "+IP);
                       list.add(type);
                       }
                       rs.close();
                       con.close();
    
                   }catch (SQLException e) {
                    System.out.println("SQLException");
                    e.printStackTrace();
                 }
             }
             else {
                System.out.println("Didn't find any data");
             }
             return(list);
           }
    
           public static Connection getConnection() {
    
                 Connection con = null;
                 String Res = "na";
                 String BusinessUnit = "NotFound";
                 ResultSet rs = null;
                 try {
                     // Load the JDBC driver
                    String driverName = "oracle.jdbc.driver.OracleDriver";
    
                    // String driverName = "oracle.jdbc.OracleDriver";
                     Class.forName(driverName);
                     // Create a connection to the database
                     //Dev
                     String serverName = "xx.xx.xx.xx";
                     String portNumber = "1521";
                     String sid = "SSSS";
                     String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
                     String username = "SSSSS";
                     String password = "password";
                     con = DriverManager.getConnection(url, username, password);
                      return con;
                      } catch (ClassNotFoundException e) {
                    System.out.println("ClassNotFoundException");
                    e.printStackTrace();
                 } catch (SQLException e) {
                    e.printStackTrace();
                }
                return con; 
              }
         }
    

    正如下面的解决方案所建议的,另一个模型类

        package com.example.model;
    
        public class Types {
            private String Name;  
            private String PCANo;
            private String IP; 
            //constructors   //getter-setters 
            public String setName(String Name){     
                return this.Name = Name;  
             }   
             public String getName() { 
                return this.Name; 
             }
             public String setPCANo(String PCANo) { 
                return this.PCANo = PCANo;  
             }  
             public String getPCANo() {  
                return this.PCANo;   
             }  
             public String setIP(String IP) { 
                    return this.IP = IP;  
                 }  
                 public String getIP() {  
                    return this.IP;   
                 }  
        } 
    

    最终的JSP显示文件

    <%@ page import="java.util.*" %>
    <%@ page import="com.example.model.Types" %> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    
    <html>
    <body>
    <h1 align="center">Final Data JSP View</h1>
    <p>
    
    <%
    
    List<Types> styles = (List<Types>) request.getAttribute("styles");
    if(styles!=null){
        for(Types type: styles){  
            out.println("<br/>" + type.getName() + " " + type.getPCANo()+ " " + type.getIP());  
            }  
        } 
    %>
                </body>
    </html>
    

    结果是只提取显示所有行的最后一行,即数据库表有三行,最后一行显示3次。

    ABC PCA100 XXX.1.0.0
    ABC PCA100 XXX.1.0.0
    ABC PCA100 XXX.1.0.0


    您必须创建一个代表NamePCANoIPmodel类。

    public class Types
    {
      private String name;
      private String pcaNo;
      private String ip;
      //constructors
      //getter-setters
    }
    

    getTypes方法返回CoffeeExpert类的List<Types>

     public List<Types> getTypes(String type) {
         Connection con = getConnection();
         String Query = "select * from ABC";
         List<Types> list=new ArrayList();
         rs = con.createStatement().executeQuery(Query);
    
          while (rs.next()) {
             Types type=new Types();
             type.setName(rs.getString(1));
             type.setPcaNo(rs.getString(2));
             type.setIp(rs.getString(3));
             list.add(type);
          }
          rs.close();
          con.close();
        return list;
      }
    

    在.jsp页面中显示List<Types>

    JSP标签:

    <%
      List<Types> styles = (List<Types>) request.getAttribute("styles");
      if(styles!=null){
        for(Types type: styles){
           out.println("<br/>" + type.getName() + " " + type.getPcaNo());
         }
      }
    %>
    

    JSTL:

    <c:forEach var="type" items="${styles}">
      <br/>
      <c:out value="${type.name}" />
      <c:out value="${type.pcano}" />
      <c:out value="${type.ip}" />
    </c:forEach>
    

    参考SO线程:

  • JSTL FAQ - 使用JSTL 1.2它需要单个http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar文件。
  • 如何避免JSP文件中的Java代码?
  • 链接地址: http://www.djcxy.com/p/76575.html

    上一篇: Resultset in JSP

    下一篇: how to send a ResultSet object in jsp back to html (javascript)?