未找到MySQL驱动程序的未知问题

时间:2016-01-29 16:30:24

标签: java mysql

我成功地连接了一个简单的java应用程序,该应用程序通过声明适当的方式打印出所有数据并将MySQL连接器jar文件导入到项目中。但是,问题在于我想通过Web应用程序(glassfish)连接到MySQL。我已经导入了MySQL连接器jar文件并对其进行了适当的声明,但我不明白为什么驱动程序没有被找到。

以下是我收到的错误:



2016 - 01 - 29T16: 22: 25.873 + 0000 | Info: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java: 1783)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java: 1633)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java: 187)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java: 125)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java: 264)
at org.apache.jsp.validate_jsp._jspService(validate_jsp.java: 56)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java: 111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java: 790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java: 411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java: 473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java: 377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java: 790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java: 1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java: 734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java: 673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java: 99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java: 174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java: 416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java: 283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java: 459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java: 167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java: 206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java: 180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java: 235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java: 119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java: 283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java: 200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java: 132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java: 111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java: 77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java: 536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java: 112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java: 117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java: 56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java: 137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java: 591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java: 571)
at java.lang.Thread.run(Thread.java: 745)




Validate.jsp



<% @page language = "java"
contentType = "text/html; charset=ISO-8859-1"
pageEncoding = "ISO-8859-1" %>
  < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd" >
<% @page import = "java.sql.*" %>
  <%
  try {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String driver = ("com.mysql.jdbc.Driver");
    Class.forName(driver); // MySQL database connection
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/ebooking" + "user=user&password=user");
    PreparedStatement pst = conn.prepareStatement("Select * from student where stu_username=? and stu_password=?");
    pst.setString(1, username);
    pst.setString(2, password);
    ResultSet rs = pst.executeQuery();
    if (rs.next())
      out.println("Valid login credentials");
    else
      out.println("Invalid login credentials");
  } catch (Exception e) {
  System.out.println(e);
  out.println("Something went wrong !! Please try again");
} %>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

确保已将mysql library文件添加到库

在用户

之前添加?

变化:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/ebooking" + "user=user&password=user");

 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/ebooking" + "?user=user&password=user");

答案 1 :(得分:0)

任何JDBC api调用都需要底层数据库支持的合适的jdbc驱动程序。

请检查您的应用程序WAR文件,其中包含MYSQL数据库的jdbc驱动程序。

如果未在JVM的类加载器中加载jar,则会发生ClassNotFoundException

答案 2 :(得分:0)

我认为你的方法getConnection有问题。这个方法正在等待三个参数。

import java.sql.Connection;
import java.sql.DriverManager;

public class JavaConnect {
    public static void ImportDriver() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public static Connection ConnectDB() {   
        Connection connection = null;
        String url = "jdbc:mysql://localhost/ebooking";
        try {
            connection = DriverManager.getConnection(url, "user", "user");
            return connection;
        } 
            catch(Exception ex) {
            ex.printStackTrace();
            System.out.println("Please try again !");
        } 

        return connection;
    }
}

我希望这会对你有所帮助!祝你有个美好的一天!

答案 3 :(得分:0)

尝试编写连接字符串,可能会添加portebooking?而不是ebooking,如下所示:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/ebooking?" +
                               "user=user&password=user");

并且不要忘记添加可用的jdbc driver