上个学期我有一个学校项目,建立了一个Web应用程序,其中包含一个Habitat for Humanity部分的数据库。我能够很好地连接和写入数据库,但是我无法读取数据库中的数据并将其放入Primefaces DataTable中。
我将添加我的Java类以从下面的数据库中读取,以及我的具有DataTable变量的HTML。
如果有人可以查看我的代码,看看他们是否发现任何错误或有任何提示/提示,我们将不胜感激。这个课程已经结束了,但我希望能够尝试找出Web应用程序的其余部分,以使其完全启动并运行,而不是仅仅将其留在过去。任何帮助将不胜感激!
UserDAO - Java类:
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.UserBean;
/**
* @author rnikolich
*/
public class UserDAO {
String driverName = "org.apache.derby.jdbc.ClientDriver";
String connStr = "jdbc:derby://localhost:1527/HabitatDatabase";
public static ArrayList<UserBean> getOwners() {
ArrayList<UserBean> ownerCollection = new ArrayList<>();
Connection DBConn = null;
try {
DBHelper.loadDriver("org.apache.derby.jdbc.ClientDriver");
String myDB = "jdbc:derby://localhost:1527/HabitatDatabase";
DBConn = DBHelper.connect2DB(myDB, "test", "test");
// UserBean usr = new UserBean();
String query = "SELECT * FROM HABITATDATABASE.OWNER";
// With the connection made, create a statement to talk to the DB
server.
// Create a SQL statement to query, retrieve the rows one by one (by
going to the
// columns), and formulate the result string to send back to the
client.
Statement stmt = DBConn.createStatement();
ResultSet rs = stmt.executeQuery(query);
// Timestamp timestamp;
// Vote aVote;
while (rs.next()) {
UserBean own = new UserBean();
own.setOwnerID(rs.getString("OwnerID"));
own.setOwnerName(rs.getString("OwnerName"));
own.setPhone(rs.getString("Phone"));
own.setEmail(rs.getString("Email"));
// or rs.getString("PL_Name");
// timestamp = rs.getTimestamp("vote_TimeStamp");
//
// // make a VoteTally object out of the values
// aVote = new Vote(PL_Name, timestamp);
// add the newly created object to the collection
ownerCollection.add(own);
}
rs.close();
stmt.close();
} catch (Exception e) {
System.err.println("ERROR: Problems with SQL select");
e.printStackTrace();
}
try {
DBConn.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return ownerCollection;
}
}
usersController - Java类:
package controller;
import dao.UserDAO;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.UserBean;
import java.io.Serializable;
@ManagedBean
@SessionScoped
public class UsersController implements Serializable {
private UserBean theModel;
private ArrayList<UserBean> owner;
/**
* *
*/
public ArrayList<UserBean> getOwner() {
UserDAO own = new UserDAO();
owner = UserDAO.getOwners();
return owner;
}
public void setOwner(ArrayList<UserBean> owner) {
this.owner = owner;
}
/**
* Creates a new instance of UsersController
*/
public UsersController() {
theModel = new UserBean();
}
public UserBean getTheModel() {
return theModel;
}
public void setTheModel(UserBean theModel) {
this.theModel = theModel;
}
}
viewOwner - XHTML:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
<h:head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<title>View Owner</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></link>
</h:head>
<h:body style="background-color: gray">
<h:panelGroup layout="block" styleClass="container">
<h:panelGroup layout="block" styleClass="row centered-form">
<h:panelGroup layout="block" styleClass="col-xs-12 col-sm-8 col-md-10 col-sm-offset-2 col-md-offset-1"
style="background-color: limegreen">
<h:panelGroup layout="block" styleClass="panel panel-default">
<center><h:graphicImage value="#{resource['images:habitat-for-humanity-logo.jpg']}" width="200" height="180"/></center>
<h:panelGroup layout="block" styleClass="panel-heading">
<center><h3 class="panel-title">View Owner</h3></center>
</h:panelGroup>
<br></br>
<h:panelGroup layout="block" styleClass="panel-body">
<h:form id="viewOwnerForm">
<h:panelGroup layout="block" styleClass="form-group">
<p:growl id="msgs" showDetail="true" />
<p:dataTable id="viewOwnerDataTable" value="#{usersController.owner}" var="o" rows="10">
<f:facet name="header">
View Owners Table
</f:facet>
<p:column headerText="OwnerID">
<h:outputText value="#{o.ownerID}" />
</p:column>
<p:column headerText="OwnerName">
<h:outputText value="#{o.ownerName}" />
</p:column>
<p:column headerText="Phone">
<h:outputText value="#{o.phone}" />
</p:column>
<p:column headerText="Email">
<h:outputText value="#{o.email}" />
</p:column>
</p:dataTable>
<br></br>
<h:commandLink>
<p:graphicImage value="#{resource['images:pdf.png']}" width="24"/>
<p:dataExporter type="pdf" target="tbl" fileName="owners"/>
</h:commandLink>
<br></br>
<br></br>
<h:link outcome="home" value="Home" styleClass="btn btn-warning btn-block" id="Home" style="background-color: blue"/>
<h:link outcome="viewHome" value="View Entry Home" styleClass="btn btn-warning btn-block" id="viewHome" style="background-color: blue"/>
</h:panelGroup>
</h:form>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
</h:panelGroup>
</h:body>
</html>