基本的直接Tomcat-MySQL链接

时间:2014-03-12 16:56:17

标签: java mysql tomcat

我已经尝试了几周,试图在Apache Tomcat 7服务器和MySQL 5之间建立一个非常基本的直接链接,但无济于事:

HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/MySQL"

这就是我的server.xml目前的样子:

<Server>
    <Service name="Catalina">
        <Connector port="80" protocol="HTTP/1.1" connectionTimeout="30000"/>
        <Engine name="Catalina" defaultHost="localhost">
            <Host name="localhost" appBase="" workDir="temp">
                <Context path="" docBase="www">
                    <Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource" username="server" password="server" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/common" maxActive="15" maxIdle="3"/>
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

这就是web.xml文件中的内容:

<web-app>
    <servlet><servlet-name>default</servlet-name><servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param><param-name>debug</param-name><param-value>0</param-value></init-param>
        <init-param><param-name>listings</param-name><param-value>false</param-value></init-param>
    </servlet>
    <servlet><servlet-name>jsp</servlet-name><servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param><param-name>fork</param-name><param-value>false</param-value></init-param>
        <init-param><param-name>xpoweredBy</param-name><param-value>false</param-value></init-param>
    </servlet>
    <servlet-mapping><servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping><servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
    </servlet-mapping>
    <session-config><session-timeout>30</session-timeout></session-config>
    <mime-mapping><mime-type>image/png</mime-type>
        <extension>png</extension>
    </mime-mapping>
    <mime-mapping><mime-type>text/html</mime-type>
        <extension>htm</extension>
    </mime-mapping>
    <mime-mapping><mime-type>text/css</mime-type>
        <extension>css</extension>
    </mime-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>
    <Context>
        <ResourceLink name="jdbc/MySQL" global="jdbc/MySQL" type="javax.sql.DataSource"/>
    </Context>
</web-app>

1 个答案:

答案 0 :(得分:0)

它看起来像一个类路径问题。

您的mysql-connector-java-*.jar应该在$CATALINA_HOME/lib文件夹中。寻找“MySQL DBCP Example" in the official How-To

OR

CLASSPATH变量中声明你的mysql jdbc jar文件,如下所示:
    SET CLASSPATH=c:\mysql\mysql-connector-java-3.0.8-stable-bin.jar;

如果它不起作用,请提供您正在阅读的教程以及您的配置文件。