Is it good practice to handle exception like this? Or there is any better alternative way to do it? The code works fine but I feel that not all the Exceptions of getAllGrades() method will be shown because of throws added infront of connection(). Please correct me if I'm wrong.
package DAO;
import java.sql.*;
import java.util.ArrayList;
public class StudentDAO {
static Connection con;
static PreparedStatement stmt;
static ResultSet rs;
public static void connection() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "xxx", "yyy");
}
public static ArrayList<String> getAllGrades() {
try {
connection();
stmt=con.prepareStatement("SELECT DISTINCT grade from student");
rs=stmt.executeQuery();
ArrayList<String> grades=new ArrayList<String>();
while(rs.next()) {
grades.add(rs.getString(1));
}
con.close();
return grades;
}
catch(SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}