JSP中的结果集 [英] Resultset in JSP

查看:104
本文介绍了JSP中的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :


  1. 基本形式:用户输入他的选择。

  2. 提交时,流程将被定向到一个servlet。

  3. 从servlet流程转到Java文件,在该文件中处理数据库检索和其他逻辑。

  4. 然后将结果发送回servlet。

  5. Servlet将结果转发给JSP进行显示。

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

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

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

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

最终的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次。

The results is fetching only the last line for all the no of rows getting displayed i.e, 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

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


推荐答案

你必须创建一个模型类,它代表名称 PCANo IP

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
}

getTypes 方法返回 List< Types> of CoffeeExpert class 。

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

显示列表< Types> 在.jsp页面中:

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

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主题:


  1. JSTL常见问题解答 - 使用JSTL 1.2它需要单个 http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar文件。

  2. 如何避免JSP文件中的Java代码?

  1. JSTL FAQ - Use JSTL 1.2 it requires single http://download.java.net/maven/1/jstl/jars/jstl-1.2.jar jar file.
  2. How to avoid Java Code in JSP-Files?

这篇关于JSP中的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆