Resultset in JSP

I need some help with forwarding resultset(rs) to a jsp . I've implemented the MVC structure in JAVA (note: I'm new in this). The logic flow for the same is below :

  • basic form : Where user enters his choice .
  • On submission the flow get directed to a servlet.
  • From servlet the flow goes to a Java file where the data base retrieval and other logic is taken care of.
  • Then the result is send back to the servlet .
  • Servlet forwards the result to a JSP for display.
  • 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); 
              }
            }
    

    The java file :

        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; 
              }
         }
    

    As suggested in solution below , Another model class

        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;   
                 }  
        } 
    

    The final JSP display file

    <%@ 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>
    

    The results is fetching only the last line for all the no of rows getting displayed ie, the database table has three rows, the last row is getting displayed 3 times.

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


    You have to create a model class which represent Name , PCANo and IP .

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

    and getTypes method returns List<Types> of CoffeeExpert class.

     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;
      }
    

    To show the List<Types> in .jsp page:

    JSP Tags:

    <%
      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>
    

    Reference SO threads:

  • JSTL FAQ - Use JSTL 1.2 it requires single http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar file.
  • How to avoid Java Code in JSP-Files?
  • 链接地址: http://www.djcxy.com/p/76576.html

    上一篇: DAO层可以返回一个JDBC ResultSet(Java)

    下一篇: JSP中的结果集