简体   繁体   English

无法使JDBC驱动程序在tomcat jsp中工作

[英]Cannot get JDBC driver to work in tomcat jsp

I try to make a simple example connection to a mysql database in jsp running on tomcat. 我尝试在tomcat上运行的jsp中建立到mysql数据库的简单示例连接。

Running the code below I get the response: No suitable driver found for jdbc:mysql://localhost/TestingDB 运行下面的代码,我得到响应:没有为jdbc找到合适的驱动程序:mysql:// localhost / TestingDB

<HTML>
    <HEAD><TITLE>Testing mysql</TITLE></HEAD>
<BODY>

<%@ page import = "java.sql.*"%>

<%  
        String url = "jdbc:mysql://localhost/TestingDB";

        String user = "mysql";
        String password = "pwd";

    try {
        Connection con = DriverManager.getConnection(url, user, password);
        out.println("Success");
        Statement st = null;
        ResultSet rs = null;
        st = con.createStatement();
        rs = st.executeQuery("SELECT * FROM TestTable");
        while(rs.next()){ 
            out.println(rs.getString("String1"));
        }

    } catch (Exception e) {
        out.println(e.getMessage());
            e.printStackTrace();
        }
%>

</BODY>
</HTML>

I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory and when it did not work I tried to put it in the {appname}/WEB-INF/lib directory. 我已经将mysql-connector-java-5.1.xx-bin.jar放在CATALINA_HOME / lib目录中,当它不起作用时,我尝试将其放在{appname} / WEB-INF / lib目录中。

I have restarted tomcat between each try using services tomcat restart . 我已经在每次尝试之间使用服务 重新启动了 tomcat

There should not be any problems accessing the database as it works fine using (pretty much) the same code in a standalone java class. 访问数据库应该没有任何问题,因为在独立的Java类中使用(几乎)相同的代码可以正常工作。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

class JDBCTest {

    private static final String url = "jdbc:mysql://localhost/TestingDB?useSSL=false";

    private static final String user = "mysql";     
    private static final String password = "pwd";

    public static void main(String args[]) {
            try {
            Connection con = DriverManager.getConnection(url, user, password);
            System.out.println("Success");
            Statement st = null;
            ResultSet rs = null;
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM TestTable");
            while(rs.next()){ 
                  System.out.println(rs.getString("String1"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I'm really running out of ideas. 我的想法真的耗尽了。 All help is welcome! 欢迎所有帮助!

I have put the mysql-connector-java-5.1.xx-bin.jar in the CATALINA_HOME/lib directory 我已经将mysql-connector-java-5.1.xx-bin.jar放在CATALINA_HOME / lib目录中

That should work, but you would have to restart Tomcat before testing it. 那应该可以,但是您必须在测试之前重新启动Tomcat。

and when it did not work I tried to put it in the {appname}/WEB-INF/ directory. 当它不起作用时,我尝试将其放在{appname} / WEB-INF /目录中。

Wrong. 错误。 Should be the {appname}/WEB-INF/lib directory. 应该是{appname}/WEB-INF/lib目录。

Howver you should be defining this database as a Resource in your META-INF/context.xml and getting it from Tomcat via a JNDI lookup on the java:comp/env/jdbc namespace. 无论如何,您应该在META-INF / context.xml中将此数据库定义为资源,然后通过java:comp/env/jdbc名称空间上的JNDI查找从Tomcat获取它。 What you're doing is poor practice. 您正在做的是不好的练习。

You forgot to put the port in the URL access. 您忘记将端口放入URL访问。

Here is a simple example for a connection URL: 这是连接URL的简单示例:

jdbc:mysql://localhost: 3306 /TestingDB; jdbc:mysql:// localhost: 3306 / TestingDB;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDBC驱动程序和Tomcat问题 - Problems with JDBC Driver & Tomcat 带有mysql的Tomcat:“无法为连接URL&#39;null&#39;创建类”的JDBC驱动程序” - Tomcat with mysql : “ Cannot create JDBC driver of class '' for connect URL 'null' ” Tomcat,MySQL-&gt;无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - Tomcat, MySQL -> Cannot create JDBC driver of class '' for connect URL 'null' 无法加载JDBC驱动程序类&#39;com.mysql.jdbc.Driver&#39;Tomcat 8和Eclipse - Cannot load JDBC driver class 'com.mysql.jdbc.Driver' Tomcat 8 & Eclipse 如何使用 tomcat、mysql 和 jdbc 驱动程序在互联网上发布我的 jsp 项目 - how to publish my jsp project on internet using tomcat,mysql and jdbc driver Tomcat 8-java.sql.SQLException:无法为连接URL&#39;jdbc:mysql:// xxx / myApp&#39;创建类&#39;&#39;的JDBC驱动程序 - Tomcat 8 - java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp' 无法加载JDBC驱动程序 - cannot load JDBC Driver 无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序:Tomcat + MySQL + Spring MVC - Cannot create JDBC driver of class '' for connect URL 'null' : Tomcat + MySQL + Spring MVC org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' Tomcat 8 + MySQL + Spring + JPA-无法为连接URL&#39;null&#39;创建类&#39;&#39;的JDBC驱动程序 - Tomcat 8 + MySQL + Spring + JPA - Cannot create JDBC driver of class '' for connect URL 'null'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM