Reputation: 1668
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?
Upvotes: 0
Views: 370
Reputation: 25950
how to improve this code with try-catch exeptions?
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.
public List<Dashboard> getDashboardList(DataSource ds)
{
List<Dashboard> list = new ArrayList<Dashboard>();
Connection con = null;
PreparedStatement ps = null;
try
{
con = ds.getConnection();
ps = con.prepareStatement("SELECT * from GLOBALSETTINGS");
//get customer data from database
ResultSet result = ps.executeQuery();
while (result.next())
{
Dashboard cust = new Dashboard();
cust.setUser(result.getString("SessionTTL"));
cust.setPassword(result.getString("MAXACTIVEUSERS"));
list.add(cust);
}
}
catch(Exception e1)
{
// Log the exception.
}
finally
{
try
{
if(ps != null)
ps.close();
if(con != null)
con.close();
}
catch(Exception e2)
{
// Log the exception.
}
}
return list;
}
Upvotes: 2
Reputation: 10463
This is pretty basic Java stuff so I recommend picking up a book and learning the basics. Regardless, I typically do something like the following
Connection conn = null;
try {
// DB code
} catch (SQLException se) {
log.error("Experienced SQLException in method foo", se);
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Oops! You messed up!", null);
FacesContext.getCurrentInstance().addMessage(null, msg);
} finally {
if (conn != null && conn.isOpen()) {
try {
conn.close();
} catch (SQLException see) {
log.error("Connection can't be closed!");
// Faces message or something like it
}
}
Upvotes: 1
Reputation: 137282
Why would you want to do that? If any error occurs, the method doesn't really have what to do, so it seems appropriate to throw an exception in those cases.
The only thing I would change is adding a finally method, to close connections:
try {
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);
}
}
finally {
ps.close();
con.close();
}
Upvotes: 2
Reputation: 77995
Not sure what you mean by proper, but if you remove the top throws SQLException
from your method, your IDE will display a tooltip for any non caught exception, and you can auto-insert each of the missing ones that way.
Upvotes: 1