Java – unable to obtain the jdbc driver of MySQL in Tomcat environment

I've been trying to get the driver class connected to MySQL's JDBC

bash-3.2$echo $CLASSPATH
/home/cmao/public_html/jsp/mysql-connector-java-5.1.12-bin.jar

This indicates that I have added the latest MySQL connection jar archive to my classpath variable

I have created a test JSP page that can be found here

The source code for this page is:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<html>
<head>
    <title>UTS JDBC MysqL connection test page</title>
</head>
<body>
<%
    Connection con = null;
    out.print("Java version is   : " + System.getProperty("java.version") + "<br />");
    out.print("Tomcat version is : " + application.getServerInfo() + "<br />");
    out.print("Servlet version is: " + application.getMajorVersion() + "<br />"); 
    out.print("JSP version is    : " + JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion() +"<br />");
    //out.print("Java classpath is : " + System.getProperty("java.class.path")+ "<br />");
    //out.print("JSP classpath is  : " + appliaction.getAttribute("org.apache.catalina.jsp_classpath") + "<br />");
    //out.print("Tomcat classpath is : " + System.getProperty("org.apache.tomcat.common.classpath") + "<br />");

    try
    {
        Class c = Class.forName("com.MysqL.jdbc.Driver");
    }
    catch(Exception e)
    {
        out.println("Error! Failed to obtain JDBC driver for MysqL... Missing class \"com.MysqL.jdbc.Driver\"<br />");
    }
%>
</body>
</html>

These commented lines are useless, and all kinds of jsper expositions will be thrown

You can check these error pages from the following link: classpath error page Catalina error page Tomcat error page

It seems that based on my limited knowledge of JSP and servlet, the Tomcat environment "ignores" my java classpath? In this case, I cannot configure the MySQL JDBC package to make my servlet (JSP is a servlet anyway) work

I don't know how to solve the problem If I use an IDE like eclipse or NetBeans and create a real Java "web application" to use web Config XML configuration file to "self configure", would that be better? Can I bypass this Tomcat environment restriction?

Thank you very much for your advice in advance

Solution

Too bad, Tomcat (and all other Java EE application servers) ignore any system classpath environment variables

You should add the jdbc driver jar in one of two places:

>Your Web context is WEB-INF / lib, which means it only applies to your application (probably not a bad thing) > if you are using version 5 x. Version 5.0 is used in Tomcat server / lib X or / lib

I believe in Tomcat 6 X requires you to put the jdbc driver jar in / lib

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>