Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble linking datatable to resultset using JSF

Status
Not open for further replies.

raindogs

Programmer
Nov 23, 2005
27
US
Hello all,

This is one of those really painful ones where I had it working, didn't save a backup, made a few changes, and now I can't figure out how to get it working again. I'm working on a JSF/PostgreSQL application and I've lifted the bulk of this portion straight from Chapter 5 of the “Core JSF” book. I cannot for the life of me figure out what is wrong here.

The page displaying the datatable filled with the resultset is “main.jsp”, and every time I load it I get the following error:

[ServletException in:/WEB-INF/pages/main.jsp] javax/servlet/jsp/jstl/sql/Result'

The page is also using data from a “Person” bean containing login information that works fine. I know the query is correct and that the DB is up and running, but for some reason my resultset/datatable connection is just dead in the water. The offending code is pasted below, any help would be much appreciated.

Thanks,
Alex

**********
main.jsp
**********
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">


<%@ taglib uri=" prefix="h" %>
<%@ taglib uri=" prefix="f" %>
<%@ taglib uri=" prefix="c" %>


<f:view>

<h2>Welcome,
<h:eek:utputText value="#{person.firstName}" escape="false"/>
</h2>


<P>I see by your outfit that you are a <h:eek:utputText value="#{person.userRole}" escape="false"/></P>
<P>Here are the farms in the database:</P>
<!-- begin fields datatable -->
<h:dataTable value="#{fieldview.farms}" var="farm">
<h:column>
<f:facet name="header">
ID
</f:facet>
<h:eek:utputText value="#{fieldview.id}"/>
</h:column>
<h:column>
<f:facet name="header">
Name
</f:facet>
<h:eek:utputText value="#{fieldview.name}"/>
</h:column>
</h:dataTable>

<!-- end fields datatable -->
</f:view>

******************************************************************
FieldView.java (backing bean that is set up in the faces-config file as “fieldview”)
******************************************************************


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import java.sql.DriverManager;


public class FieldView {
private Connection conn;

/**
* @param args
*/
public FieldView() {

}


public Result getFarms() throws SQLException, NamingException {
try {
connect();
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("select * from farm");
return ResultSupport.toResult(result);
} finally {
close();
}
}


public void close() throws SQLException {
if (conn == null) return;
conn.close();
conn = null;
}

public void connect(){
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}

try {
this.conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/<dbname>",
"<my username>", "<my password>");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top