我需要一些帮助将结果集(rs)转发到 jsp 。我已经在 JAVA 中实现了 MVC 结构(注意:我是新手)。相同的逻辑流程如下:
- 基本形式:用户输入他的选择。
- 在提交时,流程被定向到一个 servlet。
- 流程从 servlet 转到 Java 文件,在该文件中处理数据库检索和其他逻辑。
- 然后将结果发送回 servlet。
- Servlet 将结果转发到 JSP 以供显示。
小服务程序:
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