我应该如何将mysql workbench中的数据库与我的Web应用程序连接?

时间:2016-09-07 08:18:46

标签: mysql jsp servlets

我在eclipse中创建了一个Web应用程序但是我无法连接到我在sql workbench中创建的模式。所以,任何人都可以详细告诉我,我所做的事情是错误的。

这是我的连接代码: -

   try{
        Class.forName("com.mysql.jdbc.Driver");         
    Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/users","root","root");  

    Statement stmt=con.createStatement();  
    stmt.executeQuery("Insert into Regsitered_users(Name,Pass,email)" + "values('"+name+"','"+pass+"','"+email+"')");
}

但是会产生这个错误: -

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.arithmatic.pkg.Reg.doPost(Reg.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

问题是你没有com.mysql.jdbc.Driver如果你使用像Maven这样的构建工具你必须添加依赖:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.6</version>
</dependency>

否则你必须在项目中添加外部jar。您可以从此处下载:https://www.mysql.com/products/connector/

答案 1 :(得分:0)

下载mysql-jdbc jar,您可以从here下载并将其添加到bin文件夹中,如果您使用的是eclipse,请重新启动tomcat服务器,它将会工作