开发者

Configure Tomcat so that I can connect a JSP page to MySQL

开发者 https://www.devze.com 2023-01-27 15:06 出处:网络
I need to connect to a MySQL database from a JSP page by using the DriverManager.getConnection() method. I have placed the MySQL Connector-J JAR file in the Tomcat lib. I have run the same code as a n

I need to connect to a MySQL database from a JSP page by using the DriverManager.getConnection() method. I have placed the MySQL Connector-J JAR file in the Tomcat lib. I have run the same code as a normal Java application and it works, which makes me think that there is an issue with Tomcat. I am getting many exceptions, the first one being ClassNotFoundException, followd by many JasperException.

Could you tell me the steps I will need to follow in configuring the servlet to interact with MySQL from JSP page?


Update: I have tried putting in the lib folder of Tomcat install root as well as the /WEB-INF/lib, but the problem persists. The jar file name is mysql-connector-java-5.1.13-bin.jar. Is this the right one?

Here is the exception I am getting

java.lang.ClassNotFoundException: com.mysql.jdbc.driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org开发者_JS百科.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)


Assuming that the ClassNotFoundException is referring to the JDBC driver com.mysql.jdbc.Driver, then it means that the Connector-J JAR file is not been placed in the classpath properly.

In fact, placing it in the Tomcat's /lib folder should fix this problem. However since you seem to already have done this, then this can only mean that either you didn't put the right JAR file there at all, or that the /lib folder you're talking about is actually the wrong folder. In case of Tomcat 6.0 or newer, it should be the /lib folder inside the Tomcat installation folder (it should already exist, you shouldn't create one yourself!). In case of Tomcat 5.5 or older, it should be the /shared/lib folder inside Tomcat installation folder (again, it should already be there).

An alternative is to put the JAR file in /WEB-INF/lib folder of your webapplication. The only disadvantage is that the JAR file will then end up in only the classpath of the webapp used. For all other webapps in the same servletcontainer you have to duplicate the JAR file. Also, you won't be able to utilize the Tomcat-provided connection pooling facilities to improve connecting performance.


Update: as per your stacktrace:

java.lang.ClassNotFoundException: com.mysql.jdbc.driver

The classname is wrong. Java is case sensitive. It should be com.mysql.jdbc.Driver.

0

精彩评论

暂无评论...
验证码 换一张
取 消