标题:servlet连接mySQL报com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcept ...
只看楼主
番茄大帝
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2008-11-4
结帖率:80%
已结贴  问题点数:50 回复次数:2 
servlet连接mySQL报com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communic
用tomcat作为服务器,lib下有mysql连接的jar包,servlet例子程序里该加载的加载了,该连的也连了,账号密码,数据库都没写错,也加载了tomcat目录下的servlet jar包,web.xml也没搞错。可是在浏览器输入地址后,在tomcat的dos窗口就报com.mysql.jdbc.exceptions. Communications link failure这个异常了, 哪位可以帮帮呢?

注明:JDBC或不需要用到tomcat管理数据库的案例都能成功。就需要用到tomcat管理数据库的就不行了,上面那个异常到底什么意思?

import javax.servlet.*;
import javax.servlet.http.*;

import *;
import java.sql.*;

public class ShowRs extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        response.setContentType("text/html");
        response.setCharacterEncoding("gb2312");
        PrintWriter out = response.getWriter();
        
        out.println("<table border=1>");
        out.println("<tr><td>Content:</td></tr>");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/class?user=root&password=qWERdXzCfsaV");
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from news");
            while(rs.next()){
                out.println("<tr>");
                out.println("<td>" + rs.getString("cont") + "</td>");
                out.println("</tr>");
            }
            out.println("</table>");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(rs != null) {
                    rs.close();
                    rs = null;
                }
                if(stmt != null) {
                    stmt.close();
                    stmt= null;
                }
                if(conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

[[it] 本帖最后由 番茄大帝 于 2009-7-18 20:09 编辑 [/it]]
搜索更多相关主题的帖子: fai exceptions jdbc servlet link 
2009-07-16 19:14
番茄大帝
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2008-11-4
得分:0 
问题解决了,原来是端口号写错了,千找万找,原来是这个原因,这个例子就是利用servlet尝试连接数据库,有兴趣的可以down下来试一试。
2009-07-18 20:11
mahayu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:160
注 册:2007-8-3
得分:50 
路过,看看
用servlet连接数据库,蛮累的,每次调用servlet就得重新连接一次
不爽.

研究Java和C#,,Jsp,Sql等各类编程技术, QQ:442013799  QQ群:47934757
2009-07-21 12:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-279662-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031603 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved