JSP中的结果集

时间:2012-08-16 02:30:43

标签: java jsp servlets resultset

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

  1. 基本形式:用户选择的地方。
  2. 提交时,流程将定向到servlet。
  3. 从servlet流程到Java文件,在那里处理数据库检索和其他逻辑。
  4. 然后将结果发送回servlet。
  5. Servlet将结果转发给JSP进行显示。
  6. 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

1 个答案:

答案 0 :(得分:4)

您必须创建代表modelNamePCANo的{​​{1}}课程。

IP

public class Types { private String name; private String pcaNo; private String ip; //constructors //getter-setters } 方法返回getTypes类的List<Types>

CoffeeExpert

要在.jsp页面中显示 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>

JSTL:

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

参考SO线程:

  1. JSTL FAQ - 使用JSTL 1.2它需要单个http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar文件。
  2. How to avoid Java Code in JSP-Files?