【问题标题】:error calling method from jsp but not from Java class [duplicate]来自jsp但不是来自Java类的错误调用方法[重复]
【发布时间】:2018-11-30 13:25:37
【问题描述】:

在我的 jsp 文件中使用以下代码,我尝试调用 java 方法并给出输出

<%@page import="lostmusicadmin.Jobs"%>
<% Jobs j = new Jobs(); String a = j.getDBTime(); %> <%=a%> //reject2

很遗憾,我收到以下错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

在同一个项目中,我创建了文件 Main.java 并调用了相同的方法,效果很好:

public static void main(String[] args) {
        Jobs j = new Jobs();
        System.out.println(j.getDBTime()); //2018-06-21 11:08:23.0
}

我的工作.java

package lostmusicadmin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Jobs {

    private String dbHost = "localhost";
    private String dbName = "ajax";
    private String dbUser = "root";
    private String dbUserPW = "";
    private String DB_URL = "jdbc:mysql://" + dbHost + "/" + dbName;

    public String getDBTime() {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, dbUser, dbUserPW);
            ps = conn.prepareStatement("select now() as time from dual");
            ResultSet rs = ps.executeQuery();
            String time = null;
            while (rs.next()) {
                time = rs.getString("time");
            }
            return time;
        } catch (SQLException se) {
            se.printStackTrace();
            return "reject1";
        } catch (Exception e) {
            e.printStackTrace();
            return "reject2";
        } finally {
            try {
                if (ps != null)
                    ps.close();
            } catch (SQLException se2) {
                return "reject3";
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
                return "reject4";
            }
        }
    }

}

构建路径中没有缺少类

编辑: 解决方案是从 Tomcat 将 CLASS 添加到文件夹 LIB

【问题讨论】:

    标签: java mysql jsp jdbc


    【解决方案1】:

    将 mysql-jdbc.jar 放在项目的 /WEB-INF/lib 目录的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 2015-04-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      相关资源
      最近更新 更多