【问题标题】:facing issue when using JDBC in Tomcat 7 with Struts在带有 Struts 的 Tomcat 7 中使用 JDBC 时面临的问题
【发布时间】:2012-03-31 17:24:21
【问题描述】:

我正在使用 Struts 2 开发一个 Web 应用程序,我试图将一些值插入到我的本地 MySQL 数据库中。

连接和访问数据库的代码在控制台应用程序中运行良好。

但是当我在 Struts 中运行相同的代码时,它给出了 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 异常。

Connection conn = null;
          String url = "jdbc:mysql://localhost:3306/";
          String dbName = "testdatabase";
          String driver = "com.mysql.jdbc.Driver";
          try
            {
                Class.forName(driver).newInstance();
                  conn = DriverManager.getConnection(url+dbName,"root","root");

                Statement statement=conn.createStatement();
                System.out.println("HelloWorld.execute()");
                int i= statement.executeUpdate("INSERT INTO testTable VALUES('15','Lucky')");
                System.out.println("res: "+i);

            }
        catch(Exception e)
        {
            System.out.println(e);
            //setMessage(e.getMessage());
        }

【问题讨论】:

    标签: mysql jdbc struts2 tomcat7


    【解决方案1】:

    该错误意味着它无法在您的类路径中找到 JDBC 驱动程序。你有 JDBC 驱动程序吗?您需要有 MySQL JDBC 驱动程序。如果没有,可以从 MySQL 网站下载。

    如果您有驱动程序或已下载驱动程序,请确保它位于正确的位置。它需要放在你的类路径中。这通常意味着两个地方之一

    1. WEB-INF/lib 文件夹下使用 Struts 应用程序
    2. 使用应用程序服务器。对于tomcat,这通常在<TOMCAT FOLDER>/lib

    【讨论】:

      【解决方案2】:

      去这里:

      http://dev.mysql.com/downloads/connector/j/

      下载jdbc驱动并解压到tomcat lib目录下的jar

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-06
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-21
        • 2021-11-22
        • 2011-06-11
        相关资源
        最近更新 更多