Am I doing this correctly?
I am making a smallish website and I am after some clarification/guidance/feedback on if I am properly abstracting out the different functionality, linking it all back together etc?
In short: am I doing the right thing here?
I have a JSP page that is an about page attempting to pull some user info from a database. It calls a bean called ProfileProcessBean.
ProfileProcessBean will make a call to another bean, Databse bean that only handles databse calls. It takes the results set and uses it to fill out another bean that only has information about users, called Profile Bean. For each record in the results set it makes a bean adds it to a linked list. Back in the page, the LinkedList in the bean is iterated over in the JSTL and each ProfileBean is sent to a custom tag, ProfileTag which formats it to the page.
It uses a Database from MEATA-INF/context.xml
Examples:
JSP
<%-- Iterate over the results (a linked list of Profiles) from the ProfileProsessBean --%>
<c:forEach var="profileList" items="${ProfileProcessBean.profiles}">
<%-- Send each Profile to the custom tagfor formatting and outputing --%>
<tmc:profile userProfile="${profileList}"/>
</c:forEach>
The Profile ProcessBean
package com.mysite.beans;
import com.mysite.typebeans.ProfileBean;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
/**
* This is the bean that process a profile.
* It makes a call to the database and populates profileBeans with the results.
*
* @see DatabaseBean
*/
publicclass ProfileProcessBeanimplements Serializable{
ResultSet result;// The results of the database call
LinkedList<ProfileBean> listOfBeans;// The list that the results are formatted in to and the Profile Beans
// are put into
String user;// The username that can be passed in as an optional argument
public ProfileProcessBean(){
}
/**
* Process the results of a database connection that retuns a ResultSet of a statement into profile beans that are
* passed to a custom tag for output.
* <p/>
* The statment selects a profile(s) and returns them to this method as a ResultSet. The results set is then
* iterated over and each record is used to populate a ProfileBean. Once populated the ProfileBean is added to a
* LinkedList and once all records have been processed the list is returned.
*
* @return A linked list of ProfileBeans
*/
public LinkedList getProfiles(){
DatabaseBean dbaBean =new DatabaseBean();
try{
// Check if a username is passed as "user" and if it is amend the stament
if (user !=null){
result = dbaBean.executeQuery("Select * FROM \'users\' WHERE username = " + user +";");// Select a specific profile
}else{
// Otherwise use the statment that returns them all.
result = dbaBean.executeQuery("SELECT * FROM \'users\';");// Select all the profiles as no specific profile was given
}
// Iterate over the results set and add each row found as a new ProfileBean
// Set the properties from the RS
while (result.next()){
ProfileBean profileBean =new ProfileBean();// Make a new bean to be populated
profileBean.setPseudonym(result.getString("user"));
profileBean.setEmail(result.getString("email"));
//profileBean.setPicURL(result.getString("picURL"));// not in test DB, next interation perhaps
profileBean.setAbout(result.getString("about"));
listOfBeans.add(profileBean);// Add the newly populated profile to the list, it could be one or nmany, it doesnt really matter
}
}
catch (SQLException SQLEx){
System.out.println("Error in the SQL");
System.out.println(SQLEx);
}
return listOfBeans;// Return the list full of populated beans
}
}
The Database bean
package com.mysite.beans;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Contains a collection of database access related functions.
*/
publicclass DatabaseBeanimplements Serializable{
/**
* Empty constructor
*/
public DatabaseBean(){
}
/**
* Make a connection to the database and executes an SQL query.
*
* @param SQLStatementToExecute The SQL statement to execute, as a string.
* @return A ResultsSet of the result of the executed SQL query.
*/
public ResultSet executeQuery(String SQLStatementToExecute){
System.out.println("now in dbbean");
// Grap the JDBC context defined in META-INF/context.xml
// It is setup in the ennvironment context and referenced as a DataSources "jdbc/mysite
// Local variables
Connection conn;// The connection to the database
PreparedStatement prepStmt;// The statment to execute
ResultSet rs =null;// The results of the executed statment
try{
// Lookup the connection details of the database defined in the context.xml file
Context initContext =new InitialContext();// The details are stored in the context of the conputer
Context envContext = (Context) initContext.lookup("java:comp/env");// Get the environmental context that stores the details
DataSource ds = (DataSource) envContext.lookup("jdbc/mySite");// Get this specifc context
System.out.println(ds);
// Now try to execute the SQL statement
conn = ds.getConnection();// Get a connection to the database
prepStmt = conn.prepareStatement(SQLStatementToExecute);// Build a prepared stement to make things run faster, which can be put into cache
rs = prepStmt.executeQuery();// Execute the statement and put the return into a RS
conn.close();// Close the connection so it can be returned to the pool
}catch (NamingException nameEx){
System.out.println("There was a naming exception");
System.out.println(nameEx);
System.out.println(nameEx.getCause());
}catch (SQLException SQLEx){
System.out.println("There was an SQLException");
System.out.println(SQLEx);
}
// Return the result set to the calling bean
return rs;
}
}
The Profile Bean
package com.mysite.typebeans;
/**
* This is the bean that repesents an authors profile
*/
publicclass ProfileBean{
private String pseudonym;
private String email;
private String picURL;
private String about;
private String[] tags;
// Default Constructor
public ProfileBean(){
pseudonym ="Flux Cap";
email ="flux@mysite.com";
picURL ="flux";
about ="Flux keeps the world working. Things would be different without him. " +
"Anyone fancy a ride in his Delorian?";
tags =new String[]{"Time Travel","Science","Cars"};
}
public String getPseudonym(){
return pseudonym;
}
publicvoid setPseudonym(String pseudonym){
this.pseudonym = pseudonym;
}
public String getEmail(){
return email;
}
publicvoid setEmail(String email){
this.email = email;
}
public String getPicURL(){
return picURL;
}
publicvoid setPicURL(String picURL){
this.picURL = picURL;
}
public String getAbout(){
return about;
}
publicvoid setAbout(String about){
this.about = about;
}
public String[] getTags(){
return tags;
}
publicvoid setTags(String[] tags){
this.tags = tags;
}
}
Again just some feedback on if i'm doing the right thing. Exceptions in the right place? Flow is in the right way? Any other comment. Sorry English is not my best language.

