Resultset w JSP
Potrzebuję pomocy w przesyłaniu zestawu wyników (rs) do jsp. Zaimplementowałem strukturę MVC w JAVA (uwaga: jestem w tym nowy). Przepływ logiczny dla tego samego jest poniżej:
forma podstawowa: gdzie użytkownik wprowadza swój wybór.Po złożeniu przepływ jest kierowany do serwletu.Z serwletu przepływ trafia do pliku Java, w którym pobierane jest pobieranie bazy danych i inna logika.Następnie wynik jest wysyłany z powrotem do serwletu.Serwlet przekazuje wynik do JSP w celu wyświetlenia.Serwlet:
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);
}
}
Plik 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;
}
}
Jak zasugerowano w rozwiązaniu poniżej, Inna klasa modelu
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;
}
}
Końcowy plik wyświetlania 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>
Rezultatem jest pobranie tylko ostatniego wiersza, w którym wyświetlana jest liczba wszystkich wierszy, tzn. Tabela bazy danych ma trzy wiersze, ostatni wiersz jest wyświetlany 3 razy.
ABC PCA100 XXX.1.0.0
ABC PCA100 XXX.1.0.0
ABC PCA100 XXX.1.0.0