java.net.ConnectException: Connection refused: connect 雷人解决方法,希望高手解释原因

zkc5050 2009-04-14 07:19:27
源代码如下,
<%@ page import ="java.io.*"%>
<%@ page import ="java.util.*"%>
<%@ page import ="java.sql.*"%>

<%
String dbUrl="jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=GB2312";
String dbUser="root";
String dbPwd="6965050";
String driverName="com.mysql.jdbc.Driver";

Connection con=null;
Statement stmt=null;
ResultSet rs=null;

Class.forName(driverName);

con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
if(con!=null)
System.out.println("connect successfully!");

%>


错误页面
root cause

javax.servlet.ServletException: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:518)
at java.net.Socket.connect(Socket.java:468)
at java.net.Socket. <init>(Socket.java:365)
at java.net.Socket. <init>(Socket.java:208) 后面还有一大堆错误

我的环境变量什么都没有设置错误,具体我的错误和我的软件版本和环境变量可以看这个贴http://topic.csdn.net/u/20090413/18/a50872d9-7ab7-4dd0-b547-a1eb83deaaab.html

我的操作系统是!!!!!VISTA!!!!!!! 一直出现Connection refused: connect 错误,,,,,
当我把原代码中 localhost换成127.0.0.1,,,,,,奇迹出现了,竟然能访问数据库了,,,,,,,

希望高手说明这时啥原因,,,这也太雷人了吧,,,不这样试下,岂不是要冤死。。。。。。。。。。。。。是VISTA的原因???

希望有高手能发个新帖把原因什么解释下,免得再有像我这样的倒霉小白走这样的弯路。。。


...全文
40966 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxlcat 2012-11-02
  • 打赏
  • 举报
回复
你没有打开mysql服务
cmd
net start mysql
陈jack 2012-07-13
  • 打赏
  • 举报
回复
遇到了同样的问题 怎么解决啊
tomjava126 2012-01-13
  • 打赏
  • 举报
回复
那不是雷人~!是个小bug 你重启一下Eclipse 就ok 与 127.0.1 和 localhost 无关
z2010490051 2011-12-30
  • 打赏
  • 举报
回复
我也出现同样的错误,正在改正中!
「已注销」 2011-12-08
  • 打赏
  • 举报
回复
学习,以上内容,学习
st01fb 2011-12-01
  • 打赏
  • 举报
回复
no study ?
zjz8204 2011-10-15
  • 打赏
  • 举报
回复
我也遇到这问题,代码与楼主一致,换成127.0.0.1或是localhost 都不行,还是报这个错,请高手指点!
woxiangyy 2011-09-06
  • 打赏
  • 举报
回复
确实很容易
whoYouAre 2011-07-19
  • 打赏
  • 举报
回复
这分得得太容易了
lylshr 2010-07-26
  • 打赏
  • 举报
回复
学习;
zkc5050 2009-04-14
  • 打赏
  • 举报
回复
貌似是6楼高手说的问题,,,我打开系统的hosts是这样的

# copyright (c) 1993-2006 microsoft corp.
#
# this is a sample hosts file used by microsoft tcp/ip for windows.
#
# this file contains the mappings of ip addresses to host names. each
# entry should be kept on an individual line. the ip address should
# be placed in the first column followed by the corresponding host name.
# the ip address and the host name should be separated by at least one
# space.
#
# additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# for example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host


::1 localhost



我直接在localhost后面加127.0.0.1 系统不让我保存,说权限不够,但是我已经是管理员权限了,
我想如果加上了应该能解决这个问题了
susoft2008 2009-04-14
  • 打赏
  • 举报
回复
C:\WINDOWS\system32\drivers\etc 这个目录下hosts文件配置下:127.0.0.1 localhost
ben0759 2009-04-14
  • 打赏
  • 举报
回复
如果想知道是不是vista,最好就是换一个操作系统试下。。。
或者换台电脑试下吧。
Dieu_xiao 2009-04-14
  • 打赏
  • 举报
回复
试试加上try{} catch{}异常处理一下
zhj92lxs 2009-04-14
  • 打赏
  • 举报
回复
诡异
JaredJared 2009-04-14
  • 打赏
  • 举报
回复
或许是mysql没开网络访问。
cnhome 2009-04-14
  • 打赏
  • 举报
回复
拒绝连接,你的权限对么?!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧