Привет всем! Не могу понять как правильно вывести записи бд используя jsf.
Допустим есть таблица
Создал такие классы
| Код | package studentstore;
import java.sql.*; import java.util.List; import java.util.Collection; import java.util.ArrayList; import java.util.Iterator; import oracle.jdbc.driver.*;
public class DBAccess {
protected synchronized Connection getConnection() throws Exception { String driverClassName = "jdbc.oracle.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:SHERST"; String password = "TIGER"; String user = "SCOTT"; Class.forName(driverClassName); Connection con = DriverManager.getConnection(url, user, password); return con; }
public StudentInfo[] getAllStudent() throws Exception { StudentInfo[] students = null; Connection con = null; try { String selectStatement = "select * from \"StudentsStore\" "; con = getConnection(); PreparedStatement prepStmt = con.prepareStatement(selectStatement); ResultSet rs = prepStmt.executeQuery(); students = assembleStudentInfos(rs); prepStmt.close(); con.close(); } catch (Exception ex) { try { if (con != null && !con.isClosed()) { con.close(); } throw new Exception("Error executing query : " + ex.getMessage()); } catch (SQLException ex1) { throw new Exception("Could not close connection : " + ex1.getMessage()); } } return students; }
//*****************************************************************************************// private StudentInfo[] assembleStudentInfos(ResultSet rs) throws Exception{
StudentInfo st1 = new StudentInfo(); List lst = new ArrayList();
if (rs != null) { while (rs.next()) { st1.setId(new Integer(rs.getInt("ID"))); st1.setName(new String(rs.getString("Name")));
lst.add(st1); } }
StudentInfo[] st2 = new StudentInfo[lst.size()]; return (StudentInfo[])lst.toArray(st2); }
}
|
| Код | package studentstore;
public class StudentInfo {
private Integer id; private String name;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
|
| Код | package studentstore;
import java.util.*; import javax.faces.model.*; import javax.faces.context.*; import javax.faces.application.*; import javax.naming.*;
public class StudentsHran { DataModel studentModel; DBAccess stAccess; List students; Integer studentsCount;
public StudentsHran() { init(); }
public void init() { try { stAccess = new DBAccess(); students = new ArrayList(); studentsCount = new Integer(0); } catch (Exception ex) { ex.printStackTrace(); } }
public void setStudentModel(DataModel studentModel) { this.studentModel = studentModel; }
public DataModel getStudentModel() { if (studentModel == null) { studentModel = new ListDataModel(); } studentModel.setWrappedData(students);
return studentModel; }
public String select() { try { StudentInfo[] data; data = stAccess.getAllStudent();
if (data != null) { for (int i = 0; i < data.length; i++) { students.add(data); } } studentsCount = new Integer(students.size());
} catch (Exception ex) { ex.printStackTrace(); } return "selectSt"; }
}
|
файл faces-config.xml
| Код | <faces-config xmlns="http://java.sun.com/JSF/Configuration"> <navigation-rule> <from-view-id>/index.jsp</from-view-id> <navigation-case> <from-outcome>selectSt</from-outcome> <to-view-id>/index.jsp</to-view-id> </navigation-case> </navigation-rule>
<managed-bean> <managed-bean-name>reportHandler</managed-bean-name> <managed-bean-class>studentstore.StudentsHran</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean>
<managed-bean> <managed-bean-name>report</managed-bean-name> <managed-bean-class>studentstore.StudentInfo</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean>
</faces-config>
|
И недоделанный файл index.jsp
| Код | <%@ page contentType="text/html; charset=windows-1251" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html> <head></head> <body> <f:view> <h:form> <h:dataTable value="#{reportHandler.studentModel}" var="reports"> <h:column> <f:facet name="header"> <h:outputText value="StudentID"> </h:outputText> </f:facet> </h:column>
<h:column> <f:facet name="header"> <h:outputText value="StudentName"> </h:outputText> </f:facet> </h:column> </h:dataTable> </h:form> </f:view> </body> </html>
|
Как эти классы связать между собой не представляю.  |