English 中文(简体)
How to correctly use ResultSet with h:dataTable
原标题:

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data into some HashMap/ArrayList combo. Is there a good way to deal with this problem?

最佳回答

Indeed, you should always acquire and close the Connection, Statement and ResultSet in the shortest possible scope (preferably already inside the very same method block) and you should never pass any of them outside the DAO class. You need to map the ResultSet to a List<Data> wherein Data represents each row of the table. Here s a basic example how to map a resultset:

List<Data> items = new ArrayList<Data>();
...
while (resultSet.next()) {
    Data item = new Data();
    item.setColumn1(resultSet.getString("column1"));
    item.setColumn2(resultSet.getString("column2"));
    items.add(item);
}
...
return items;

Then you can just use it in the h:dataTable s value attribute.

For more examples and insights you may find one or both of the following articles useful:
http://balusc.blogspot.com/2006/06/using-datatables.html
http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html

Good luck.

问题回答

Please check the following link: http://www.coderanch.com/t/478265/JSF/java/Displaying-array-data-tables#2585794

I am sure helps you to resolve your problem

Are you using ResultDataSetModel? If yes note that in javadoc stays Note that the specified ResultSet MUST be scrollable.

Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

Here is a complete example of ResultDataSetModel usecase.





相关问题
MaxPooledStatements setting in JDBC oracle

I can t figure out how to set MaxPooledStatements in Oracle using the Oracle thin JDBC driver. Could someone point me in the right direction?

pass ResultSet from servlet to JSP

I am doing the following in my SampleServlet.java //Fill resultset from db .... try { ArrayList Rows = new ArrayList(); while (resultSet.next()){ ArrayList row = new ArrayList(); ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

Mysql session variable in JDBC string

am using this connection string to connect to mysql from java: jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8 is it possible to set the ...

热门标签