IntelliJ java.lang.ClassNotFoundException:com.mysql.jdbc.Driver尽管它在IML中

时间:2018-09-20 19:18:47

标签: java mysql intellij-idea jdbc

PS。我尝试了其他所有解决方案。没用

以下是我尝试运行的JSP代码的一部分:

<%
    try {
        Connection connection = DatabaseConnection.getConnection();
        System.out.println("Creating statement...");
        Statement stmt = connection.createStatement();
        String sql;
        sql = "SELECT CARNAME , RENT , SPECS FROM Car";
        ResultSet rs = stmt.executeQuery(sql);

        //STEP 5: Extract data from result set
        while (rs.next()) {
            //Retrieve by column name
            String car = rs.getString("CARNAME");
            float rent = rs.getFloat("RENT");
            String features = rs.getString("SPECS");

            //Display values
            System.out.println(car + " " + rent + " " + features);
        }
        //STEP 6: Clean-up environment
        rs.close();
        stmt.close();
        DatabaseConnection.closeConnection();
    }
    catch (Exception ex) {
        System.out.println("Hello");
    }
%>

这里DatabaseConnection涉及到此类:

public class DatabaseConnection {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/ZoomCar";

    static final String USER = "root";
    static final String PASS = "root";

    public static Connection connection = null;

    public static Connection getConnection() {
        if (connection == null) {
            try {
                Class.forName(JDBC_DRIVER);

                System.out.println("Connecting to Database");
                connection = DriverManager.getConnection(DB_URL, USER, PASS);

                return connection;
            } catch (ClassNotFoundException | SQLException ex) {
                ex.printStackTrace();
            }
            return null;
        }
        else {
            return connection;
        }
    }

    public static void closeConnection() {
        try {
            connection.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

查看Glassfish服务器日志,可以发现错误发生在Class.forName()内的语句DatabaseConnection上。这是我的项目IML文件的样子。我肯定在其中包含了MySQL连接器(请看</component>之前的那一行)。

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/" />
        </webroots>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" scope="PROVIDED" name="GlassFish 5.0.0" level="application_server_libraries" />
    <orderEntry type="library" name="mysql:mysql-connector-java:8.0.12" level="project" />
  </component>
</module>

0 个答案:

没有答案