Anyone know how to fix the following errors
unreported exception javax.naming.NamingException; must be caught or declared to be thrown
Context context = new InitialContext();
Auth.java:46: unreported exception java.sql.SQLException; must be caught or declared to be thrown conn = ds.getConnection();
that I get from this java servlet?
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;
public class ABC extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection conn;
CallableStatement cs;
String xy = req.getParameter("xy");
String zz = req.getParameter("zz");
// call stored procedure
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("jdbc/mypool");
conn = ds.getConnection();
cs = conn.prepareCall( "{call mysproc (?,?)}" );
cs.setString(1, xy);
cs.setString(2, zz);
cs.execute();
if ( conn != null ) {
try { conn.close(); } catch ( Exception ex ) {}
conn = null;
}
// Set the content type (MIME Type) of the response.
res.setContentType("text/html");
// Write the HTML to the response
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>my title</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>my header</h2>");
out.println("my body text<br/>");
out.println("</body>);
out.println("</html>");
out.flush();
out.close();
}
public void destroy() {
}
}
If I try to replace
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
with
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws Exception {
or
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException, SQLException, NamingException {
they both produce errors saying I cannot override doGet, as overridden method does not throw Exception, or SQLException, or NamingException.
new InitialContext()throws a checked exceptionNamingException, which should either be caught or the method where you are using this code should have a throws clause associated with it.Since you are extending
HttpServletand overridingdoGetmethod you cannot attach new checked Exception, as it is against the law of overriding in Java.Instead place the code inside a try catch block and catch
NamingException.So instead of
replace this by
Similarly
dataSource.getConnectionthrows a checked exceptionSQLExceptionwhich should be caught or rethrown, once again you cannot add new checked exception to your doGet method because of rules of overriding you will have to catch it explicitly.Rules of Overriding in Java:
Overridden Method