• Post Reply Bookmark Topic Watch Topic
  • New Topic

p:selectOneMenu - JSF, primefaces, JDBC

 
askat askarov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a working example of discharge data p: selestOneMenu of database (jdbc)




how to bind to the cat.add(new SelectItem(rs.getInt("id"), rs.getString("name"))); getters and setters? Thank you!




 
Tim Holloway
Bartender
Posts: 18418
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Askat!

I don't see anything wrong with the actual code, but there are a few adjustments that I recommend.

First, there's package "DBConnect". The Java conventions discourage using an initial capital letter for a package name component. A more correct name, therefore would be "dbconnect". Better still would be something more unique. For example, my package names usually look something like "com.mousetech.webapps.myapp.database", following the common convention of using the domain name of your employer or school or whatever group you are doing the application for.

Failure to adhere to the convention won't cause your program to fail, but it can confuse people and some automated code tools might not work properly.

Your "getCategoryName" method looks functionally OK, but "get" methods can be called up to 5 or 6 times on EACH page request, so it's better to move the actual data fetch and SelectItemList builder code to a separate method and cache the results. Something like this:



I prefer "categoryNameList" over simply "categoryName" because often "categoryName" is the name that I give for the selected item property, thus the selection and selectitem list share a common element in their property names while still having distinct names.

Note that one advantage of this caching mechanism (besides performance) is that any time you want to update the categoryNameList, all you have to do is reset categoryNameList to null. The next "get" operation will refresh it. This also makes it easier to set things up on initial page load. It's easier than tryung to do it in a @PostConstruct method.

One thing I did notice was that you didn't allow for freeing resources ("con") on an error. This would have made your code leak connections. Ideally you should also explicitly close the Statement in the same way, although closing the Connection should close the Statement, just as closing the Statement should close the ResultSet. This is one reason why I like the Spring Framework. It handles this sort of "grunt" work automatically.

A professional app should also be using a logger instead of the System.out and System.err channels, which do not have definite definition within web applications. So instead of:


Logger could would look more like this:
 
askat askarov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Welcome to the JavaRanch, Askat!

I don't see anything wrong with the actual code, but there are a few adjustments that I recommend.

First, there's package "DBConnect". The Java conventions discourage using an initial capital letter for a package name component. A more correct name, therefore would be "dbconnect". Better still would be something more unique. For example, my package names usually look something like "com.mousetech.webapps.myapp.database", following the common convention of using the domain name of your employer or school or whatever group you are doing the application for.

Failure to adhere to the convention won't cause your program to fail, but it can confuse people and some automated code tools might not work properly.

Your "getCategoryName" method looks functionally OK, but "get" methods can be called up to 5 or 6 times on EACH page request, so it's better to move the actual data fetch and SelectItemList builder code to a separate method and cache the results. Something like this:



I prefer "categoryNameList" over simply "categoryName" because often "categoryName" is the name that I give for the selected item property, thus the selection and selectitem list share a common element in their property names while still having distinct names.

Note that one advantage of this caching mechanism (besides performance) is that any time you want to update the categoryNameList, all you have to do is reset categoryNameList to null. The next "get" operation will refresh it. This also makes it easier to set things up on initial page load. It's easier than tryung to do it in a @PostConstruct method.

One thing I did notice was that you didn't allow for freeing resources ("con") on an error. This would have made your code leak connections. Ideally you should also explicitly close the Statement in the same way, although closing the Connection should close the Statement, just as closing the Statement should close the ResultSet. This is one reason why I like the Spring Framework. It handles this sort of "grunt" work automatically.

A professional app should also be using a logger instead of the System.out and System.err channels, which do not have definite definition within web applications. So instead of:


Logger could would look more like this:



and you can throw your option example?
 
Tim Holloway
Bartender
Posts: 18418
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean when you say "throw your option example"?
 
askat askarov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:What do you mean when you say "throw your option example"?



how to pull data from the database in the p: selectOneMenu (combobox) without hibernate?
 
Tim Holloway
Bartender
Posts: 18418
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF doesn't care if you use Hibernate or not. It leaves all of the database work to whatever product - or no product - you want to use to talk to the database. The JSF-specific code remains the same.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!