I have this java code which is a part of a managed bean which is used to display data from database into JSF table.
//connect to DB and get customer list
public List<Dashboard> getDashboardList() throws SQLException {
if (ds == null) {
throw new SQLException("Can't get data source");
}
//get database connection
Connection con = ds.getConnection();
if (con == null) {
throw new SQLException("Can't get database connection");
}
PreparedStatement ps = con.prepareStatement(
"SELECT * from GLOBALSETTINGS");
//get customer data from database
ResultSet result = ps.executeQuery();
List<Dashboard> list = new ArrayList<Dashboard>();
while (result.next()) {
Dashboard cust = new Dashboard();
cust.setUser(result.getString("SessionTTL"));
cust.setPassword(result.getString("MAXACTIVEUSERS"));
//store all data into a List
list.add(cust);
}
ps.close();
con.close();
return list;
}
I want to improve this code and insert try catch statements. What is the proper way to do this?
You can review your question title, since you are not asking about improvement, but maybe organizing. Based on your code, I changed your method so that it looks cleaner and more organized about catching exceptions.