我与ServletContextListener的数据库连接存在问题

时间:2018-02-04 16:04:45

标签: java mysqlconnection

我已经将sql连接器.jar添加到项目构建路径中,但是当我启动tomcat服务器时,它给了我以下错误。

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.sql.*;
import java.util.*;
@WebListener
public class MyListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event)  { 
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("driver not found");
            }
            try {
                Connection con = DriverManager.getConnection("jdbc:mysql://http://localhost:1080/users", "root", "root");
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("not connected");
            }       
    }   

public void contextDestroyed(ServletContextEvent arg0)  { 
     System.out.println("project undeployed...");     
}
}

我已经将sql连接器.jar添加到项目构建路径中,但是当我启动tomcat服务器时,它给了我以下错误。

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at MyListener.contextInitialized(MyListener.java:18)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
driver not foundjava.sql.SQLException: No suitable driver found for jdbc:mysql://http://localhost:1080/users
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MyListener.contextInitialized(MyListener.java:25)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)

    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
not connected
Feb 04, 2018 5:15:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 04, 2018 5:15:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 04, 2018 5:15:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5768 ms 

2 个答案:

答案 0 :(得分:1)

您的JDBC string

不正确
jdbc:mysql://http://localhost:1080/users

代替

jdbc:mysql://localhost:1080/users

另外,请确保项目路径中有连接器jar。

答案 1 :(得分:0)

你应该在[项目路径] / WEB-INF / lib中找到jar,运行mysql的更好方法是使用不带Class.forName的DriverManager,它将在getConnection中调用