如何解决java.lang.Class.forName(未知来源)错误?

时间:2017-03-25 14:20:18

标签: java mysql eclipse servlets

我正在尝试使用JDBC在Servlet上创建一个项目。但是错误日志中存在一些持久性错误,例如java.lang.Class.forName(Unknown Source)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)java.lang.Thread.run(Unknown Source)等。如何解决此类错误? 我正在使用Apache Tomcat v7.0,mySQL社区服务器v5.7和Eclipse IDE。

这是我的错误日志:

java.lang.ClassNotFoundException: com.mysql.driver.JDBC
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Register.register(Register.java:10)
    at RegisterServlet.doPost(RegisterServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

这是JDBC文件代码(Register.java):

import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Register {
    public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
    public static final Pattern VALID_PHONE_NUMBER_REGEX = Pattern.compile("[7-9][0-9]\\d{9}"); // ,Pattern.CASE_INSENSITIVE);
    public static int register(String name,String user,String pass,String cpass,String email, String phone,String city,String state) throws ClassNotFoundException{
        int status=0;
        try{
            Class.forName("com.mysql.driver.JDBC");
            Connection con=DriverManager.getConnection("jdbc:sqlite:localhost:8080/Diary.db");
            System.out.println("Database connected successfully!!");
            PreparedStatement ps=con.prepareStatement("INSERT INTO Trial(name, user, pass, email, phone, city, state, authorized) values(?,?,?,?,?,?,?,?)");
            if(pass!=null && cpass!=null && phone!=null && name!=null && user!=null && city!=null && state!=null){
                Matcher matcher1 = VALID_EMAIL_ADDRESS_REGEX .matcher(email);
                Matcher matcher2 = VALID_PHONE_NUMBER_REGEX .matcher(phone);
                if((pass==cpass) && (matcher1.find()) && (matcher2.find())){
                    ps.setString(1,name);
                    ps.setString(2,user);
                    ps.setString(3,pass);
                    ps.setString(4,email);
                    ps.setString(5,phone);
                    ps.setString(6,city);
                    ps.setString(7,state);
                    ps.setString(8,"yes");

                    status=ps.executeUpdate();
                }
                else{
                    status = 0;
                }
            }
        }catch(SQLException e){e.getMessage();}

        return status;
    }
}

这是我的servlet(RegisterSevlet.java):

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        int status;
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();        
        String name=req.getParameter("name");
        String user=req.getParameter("user");
        String pass=req.getParameter("pass");
        String cpass=req.getParameter("cpass");
        String email=req.getParameter("email"); 
        String phone=req.getParameter("phone");
        String city=req.getParameter("city");
        String state=req.getParameter("state");
        try {
            status = Register.register(name, user, pass, cpass, email, phone, city, state);             
            if(status > 0){
                out.print("<p>You are successfully registered!</p>");
                req.getRequestDispatcher("login.html").include(req, res);

            }
            else if(status == 0){
                out.println("<p>Enter the information correctly!</p>");
                res.sendRedirect("register.html");
            }

            req.getRequestDispatcher("#").include(req, res);
            out.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}

2 个答案:

答案 0 :(得分:0)

`Class.forName()` 

抛出ClassNotFoundException  您需要包含多语句声明(SQLExceptionClassNotFoundException的相同捕获) 您可以将catch语句修改为:

catch (ClassNotFoundException | SQLException e) 
       {
           e.printStackTrace();
       }

试试这个。您可以在任何需要捕获多个异常的地方使用multicatch语句。

答案 1 :(得分:-1)

只要类在编译时存在但在运行时不存在,就抛出此异常。 请检查在运行时你的classpath是否正确,你指向有问题的库(在你的情况下是mysql连接器jar)。