0

Using the following code in my jsp-File I try to call a java method and give an output

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

Sadly I get the following error:

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

Within the same project I created the file Main.java and call the same method which works fine:

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

My Jobs.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";
            }
        }
    }

}

Class is NOT missing within build path

EDIT: Solution was adding CLASS to folder LIB from Tomcat

piguy
  • 516
  • 3
  • 10
  • 30

1 Answers1

0

place mysql-jdbc.jar at the location to /WEB-INF/lib directory of your project.

shan
  • 186
  • 2
  • 14