我在Eclipse里用Servlet 连接My sql时出现以下错误不知为何?谢谢/*type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Io 异常: The Network Adapter could not establish the connection
com.tarena.serv.resource.JdbcServlet.doGet(JdbcServlet.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.25 logs.*/

解决方案 »

  1.   

    1 MySQL 没有启动
    2 MySQL 不再你指定的主机上
    3 查一下你的网络吧,也许你根本无法连接那台机器的MySQL, 比如,打开了防火墙。
      

  2.   

    用的是自己的PC,且其他JDBC程序可以访问MYSQL,不知是何问题,以下是源码,望指教。
    内容:显示user信息listpackage com.tarena.serv.resource;import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.sql.*;public class JdbcServlet extends HttpServlet
    {
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
                 throws ServletException, IOException
       {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
       
         try
         {
         con = getConnectionByMysql();
         System.out.println("OK");
         st = con.createStatement();
         rs = st.executeQuery("select * from user_tbl");
         showUsers(response, rs);
         }catch(Exception e)
         {
         e.printStackTrace();
         throw new ServletException(e.getMessage());
         }finally
         {
         close(rs, st, con);
         }
       }    
      
       public void doPost(HttpServletRequest request,
                        HttpServletResponse response)
                 throws ServletException, IOException
       {
        doGet(request, response);
       }   
       
       private Connection getConnection() throws Exception
       {
        try
        {
        Class.forName("oracle.jdbc.driver.OracleDriver");
         return DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.23:1521:tarena", "sd0708", "sd0708");
       }catch(ClassNotFoundException e)
       {
        System.out.println("can not found oracle.jdbc.driver.OracleDriver");
        throw e;
          }catch(SQLException e)
       {
        System.out.println("can not connect databse using jdbc:oracle:thin:@192.168.0.23:1521:tarena sd0708/sd0708");
        throw e;
          }
       }
       
    // 建立DB连接
    private  Connection getConnectionByMysql()throws Exception
    {
    try{

    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://127.0.0.1:3306/test";
    String username="root";
    String pwd="123";
    return DriverManager.getConnection(url,username,pwd);
    }catch(ClassNotFoundException e){

      System.out.println("can not found com.mysql.jdbc.Driver");
    throw e;
    }catch(SQLException e)
       {
        System.out.println("can not connect databse using jdbc:mysql://127.0.0.1:3306/test root/123");
        throw e;
          }

    }
       
       private void showUsers(HttpServletResponse response, ResultSet rs) throws Exception
       {
        response.setContentType("text/html;charset=gbk");
        PrintWriter out = response.getWriter();
       out.println("<html>");
    out.println("<head><title>user list</title></head>");
    out.println("<body>");
    out.println("<h3 align=\"center\">User List</h3>");
    out.println("<hr>");
    out.println("<table align=\"center\" border=\"1\" cellpadding=\"3\" width=\"650\">");
    out.println("<tr>");
    out.println("<th>no</th>");
    out.println("<th>username</th>");
    out.println("<th>gender</th>");
    out.println("<th>hobbies</th>");
    out.println("<th>province</th>");
    out.println("</tr>");

    int no = 1;
       while(rs.next())
       {
       out.println("<tr>");
    out.println("<td align=\"center\">" + no + "</td>");
    out.println("<td align=\"center\">" + rs.getString("uname") + "</td>");
    out.println("<td align=\"center\">" + rs.getString("gender") + "</td>");
    out.println("<td align=\"center\">" + rs.getString("hobbies") + "</td>");
    out.println("<td align=\"center\">" + rs.getString("province") + "</td>");
    out.println("</tr>");
    no++;
       }
      
       out.println("</table>");
    out.println("</body>");
    out.println("</html>");
       }
       
       private void close(ResultSet rs, Statement st, Connection con)
       {
        try
       {
       rs.close();
       }catch(Exception e)
       {
       }
      
       try
       {
       st.close();
       }catch(Exception e)
       {
       }
      
       try
       {
       con.close();
       }catch(Exception e)
       {
       }
       }
    }
      

  3.   

    在cmd里面输入   mysql   看看吧! 
    或者用开始菜单里面的   mysql   command   line   client   看看!
      

  4.   

    如果没问题,换一下你的MySQL 的JDBC驱动吧!getConnectionByMysql() 这部分代码没有任何问题