PostgreSQL connection in Java Servlet to Retrieve Information from the DB. Getting Error

懵懂的女人 提交于 2019-12-01 01:44:38

I get the following error any body knows why?

The reason why you are getting that error body is that your handler for ClassNotFoundException is throwing away the original exception stacktrace. You are writing the original exception's message to System.err but (obviously) that won't go into the normal system logs. (You'll probably find find the message in the "catalina.out" file ... depending on how Tomcat is configured / launched.)

The right way to do this is to either chain the exception; e.g.

    throw new ServletException("Class not found Error", e);

or log it explicitly at the point that you catch it. And if you do log it explicitly, make sure that you log the exception, and not just the exception message.


We can't tell you the actual root cause of the problem unless we see the original exception stacktrace ... but the two most likely causes are:

  • The classloader can't find a class that is needed; e.g. because the JAR file is not in the right place.
  • Class initialization failed for some class during the initialization of the class that you are attempting to load.

Tomcat expects that your servlet will be in a package. Yours appears to be in the default package; please add one and recompile. I'll bet things will go better for you then.

Tomcat 6 and 7 expect to find JDBC JARs in the server /lib directory. Add your PostgreSQL JDBC JAR to the server /lib location.

Seem you did't add Postgresql JDBC driver into classpath, as the following error suggest:

javax.servlet.ServletException: Class not found Error

Which is origin from:

throw new ServletException("Class not found Error");

You can download Postgresql JDBC driver and put in PROJECT/WEB-INF/lib folder.

Suggestion

One suggestion, your exception handling is wrong, you should change

throw new ServletException("Class not found Error");

to

throw new ServletException("Class not found Error", e);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!