源代码如下, 
<%@ 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的原因???希望有高手能发个新帖把原因什么解释下,免得再有像我这样的倒霉小白走这样的弯路

解决方案 »

  1.   

    试试加上try{} catch{}异常处理一下
      

  2.   

    如果想知道是不是vista,最好就是换一个操作系统试下
    或者换台电脑试下吧。
      

  3.   

    C:\WINDOWS\system32\drivers\etc 这个目录下hosts文件配置下:127.0.0.1       localhost
      

  4.   

    貌似是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    系统不让我保存,说权限不够,但是我已经是管理员权限了,
    我想如果加上了应该能解决这个问题了
      

  5.   

    我也遇到这问题,代码与楼主一致,换成127.0.0.1或是localhost  都不行,还是报这个错,请高手指点!
      

  6.   

    那不是雷人~!是个小bug 你重启一下Eclipse 就ok 与 127.0.1 和 localhost 无关 
      

  7.   

    你没有打开mysql服务
    cmd 
    net start mysql