无法通过servlet连接到mysql [英] Cannot connect to mysql via servlet

查看:352
本文介绍了无法通过servlet连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了两天的时间试图找出为什么我的servlet没有连接到MySQL数据库。

I have spent 2 days trying to figure out why my servlet does not connect to the MySQL database.

我安装了MySQL并且正常工作和Eclipse。

I have MySQL installed and working properly and Eclipse.

每当我尝试建立连接时,我得到 com.mysql的 ClassNotFoundException 。 jdbc.Driver ,实际上是正确导入的。我正在使用的连接器是 mysql-connector-java5.1.14 作为外部jar正确添加,所以一切似乎都很好。这是我的代码:

Whenever I try to establish a connection, I get a ClassNotFoundException for the com.mysql.jdbc.Driver, which is actually properly imported. The connector I'm using is the mysql-connector-java5.1.14 added properly as an external jar, so everything seems fine. Here's my code:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String dbUrl="jdbc:mysql://localhost:3306/test";
  String username="root";
  String password="";



  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn=DriverManager.getConnection(dbUrl);
   System.out.println("Connected!");

  } catch (SQLException e) {
   e.printStackTrace();
   System.out.println("not connected");

  } catch(ClassNotFoundException x){
   x.printStackTrace();

  } catch(Exception e){

   e.printStackTrace();
  }


 }

这是部分堆栈跟踪:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Class.java:164)

我正在遵循已发布的Java书籍中的连接步骤。在论坛和教程中,我只看到相同的代码,无法弄清楚为什么会抛出异常。

I'm following the connection steps from a published Java book. In forums and tutorials, I just see the same code, cannot figure out why that exception is thrown.

在服务器上没有运行的普通应用程序上,异常是抛出和连接(以完全相同的方式)成功。

On a normal application which does not run on the server the exception isn't thrown and the connection (in the exact same way) its successful.

你有什么建议吗?

推荐答案

两种可能的解决方案:

1)创建WAR时,该驱动程序是否在内?你可以解压缩它(将.war文件重命名为.zip,看看它应该在/ WEB-INF / lib下。)Eclipse可能没有将它导出到WAR。

1) When you create the WAR, is that driver inside? You can unzip it (rename the .war file to .zip and just take a look -- it should be under /WEB-INF/lib.) Eclipse might not be exporting it to the WAR.

2)如果你使用Tomcat,你有没有把它放到Tomcat lib目录中?如果没有,你要部署到哪个servlet容器?

2) Did you put it into the Tomcat lib directory, if you're using Tomcat? If not, which servlet container are you deploying into?

有一点我注意到有时候,Java不喜欢有类空间。您应该能够通过将其包含在两个解决方案中的任何一个中来避免它,因此它会成为相对路径,但您可以尝试消除空间并查看它是否有帮助。

One thing I've noticed is that sometimes, Java doesn't like there being spaces to a class. You should be able to avoid it by including it in either of the two solutions so it becomes a relative path, but you can try to eliminate the space and see if it helps.

这篇关于无法通过servlet连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆