Can';t从tomcat连接到MySQL数据库

Can';t从tomcat连接到MySQL数据库,mysql,jsp,connection,tomcat6,Mysql,Jsp,Connection,Tomcat6,嘿,我得到了这个错误: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up. 我只是想连接到数据库。使用此代码 <%@page import="java.sql.*"%> <% try{ // Class.for

嘿,我得到了这个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server. 
Attempted reconnect 3 times. Giving up.
我只是想连接到数据库。使用此代码

<%@page import="java.sql.*"%>

<%
try{
//  Class.forName("com.mysql.jdbc.Driver");
    Class.forName("org.gjt.mm.mysql.Driver");

    out.println("found");
} catch (ClassNotFoundException ex){
    out.println("Erro<br/>");
    out.println(ex.toString());
} catch (Exception e){
    out.println(e.toString());

}

Connection ocon;

try{
ocon = DriverManager.getConnection("jdbc:mysql://localhost/cpjcoimbra?autoReconnect=true", "*****", "*****"); //password matches
out.print("connected");
} catch (Exception e){
    out.println(e.toString()+"<br/>");
}

%>
有人知道为什么会出现这个错误吗

编辑: mysql服务状态提供以下信息:

 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.1.37, for debian-linux-gnu on i486
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version      5.1.37-1ubuntu5
Protocol version    10
Connection      Localhost via UNIX socket
UNIX socket     /var/run/mysqld/mysqld.sock
Uptime:         1 hour 32 min 21 sec

Threads: 1  Questions: 103  Slow queries: 0  Opens: 171  Flush tables: 1  Open tables: 41  Queries per second avg: 0.18

驱动程序错误-使用“com.mysql.jdbc.driver”类

JSP中的Scriptlet代码?哦,天哪,那不好

但这些不是你问题的原因

您是否授予该用户名和密码的权限?以下是示例步骤:

  • 以root用户身份登录:mysql–h localhost–u root–p;密码=
  • “创建数据库x;”
  • “创建由'q'标识的用户p;”
  • “将x.*上的所有权限授予'x'@'”
  • 提供连接到数据库时用于创建用户的用户名p和密码q

    确保服务已启动并正在侦听端口3306。打开命令shell并键入“netstat-a”,然后在端口3306上查找侦听器

    MySQL bug数据库中的错误也可能与此相关

    为了将来的参考,我发现把我收到的任何错误信息粘贴到谷歌上都很有帮助。我可能不是第一个遇到特殊问题的人

    即使您成功地解决了这个问题并使其发挥作用,这仍然是一个有致命缺陷的设计。JSP直接连接到数据库-除了您以明文输入的用户名和密码之外,没有任何安全性。你真的不想这么做

    你的连接有问题。对于初学者,请将其与JSP分开

  • 可以在命令shell中使用MySQL管理工具进行连接吗
  • 您能否编写一个简单的Java类来成功连接到数据库

  • 我将MySQL从5.1降级到5.0,现在一切正常。一定是5.1版本中的某种错误


    很好,感谢您的帮助:)

    我尝试了这两个驱动程序,但两个驱动程序的错误都是一样的。我找到的唯一错误是:unix 2[ACC]流侦听4787/var/run/mysqld/mysqld.sock我尝试将连接更改为:4787,但仍然出现相同的错误。是的,我确实正确地创建了用户。那么他们在我提供的链接中引用的主机文件呢?你改变了吗?我在mysql-connector-java-5.1.6-bin.jar中找不到驱动程序,也找不到其他地方:\+1寻求帮助,虽然是的,我在几个小时前找到了,并将其添加到了hosts.allow中,但还是什么都没做,我开始绝望了:我只是在mysql-connector-java-5.1.6-bin.jar中查找com.mysql.jdbc.driver,我可以在我的机器上看到它。
     * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.1.37, for debian-linux-gnu on i486
    Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
    This software comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to modify and redistribute it under the GPL license
    
    Server version      5.1.37-1ubuntu5
    Protocol version    10
    Connection      Localhost via UNIX socket
    UNIX socket     /var/run/mysqld/mysqld.sock
    Uptime:         1 hour 32 min 21 sec
    
    Threads: 1  Questions: 103  Slow queries: 0  Opens: 171  Flush tables: 1  Open tables: 41  Queries per second avg: 0.18