java.sql.SQLException: Communication failure during handshake. Is there a server
 running on localhost:3306?上面是出错信息,我断定是连接mysql语句出错,应该是端口被占用,可是我的机子上就一个mysql呀不解,求助高手

解决方案 »

  1.   

    <%@ page language="java" contentType="text/html;charset=gb2312" %>
    <%@ page import="java.sql.*,com.am.*"%>
    <jsp:useBean id="dbconn" scope="application" class="com.am.OpenDB"/>
    <jsp:useBean id="userlogin" scope="session" class="com.am.login" />
    <html>
    <head>
    <title>刊物文章管理系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
    <%
        userlogin.userName=request.getParameter("txtloginName");
        userlogin.userPassword=request.getParameter("txtpassword");
    userlogin.systemRole=request.getParameter("userType");
        userlogin.setConnection(dbconn.getConnection());
    if (userlogin.isValidUser())
        {
         session.setAttribute("loginType",userlogin.systemRole);
    if(0 == userlogin.systemRole.compareTo("0"))
    {
    response.sendRedirect("admin/adminManagement.jsp");
    }
    if(0 == userlogin.systemRole.compareTo("1"))
    {
    response.sendRedirect("author/articlesManagement.jsp");
    }
    if(0 == userlogin.systemRole.compareTo("2"))
    {
    response.sendRedirect("audit/auditManagement.jsp");
    }
    }
        else
        {
    %>
    <center>
    <font size="5" color="#FF0000">
    <b>登陆失败,请检查您的用户名和密码</b>
    </font>
    </center>
    <br><br>
       <center>
       <input type="button" name="goback" value="返回" onClick="javascript:window.history.go(-1)">
       </center>
    <%
        }
    %>
    </body>
    </html>
    当访问这个页面时,就会出上面的错误,
    应该是javabean出错了,因为bean中有操作库的语句
      

  2.   

    这个是openDBpackage com.am;
    import java.sql.*;public class OpenDB
    {
        
        String userName="root";
        String userPassword="root";
        private String driverName = "org.gjt.mm.mysql.Driver";
        private String url = "jdbc:mysql://localhost:3306/am";    Connection dbConn;
        public OpenDB()
        {    }
        public Connection getConnection()
        {
            try
            {
                Class.forName(driverName);
                dbConn=DriverManager.getConnection(url,userName,userPassword);
            }
            catch(Exception ex)
            {
                System.out.println(ex.toString());
                dbConn = null;
            }
           
            return dbConn;
        }}
      

  3.   

    Loginpackage com.am;
    import java.util.*;
    import java.sql.*;
    import java.text.*;
    public class login extends user
    {
     
      
        private String strSql;
        private Connection dbConn;
        private int errNum;
        private String errDesc;
        SimpleDateFormat dateFormatter;
        public login()
        {
            super();        
        strSql="";       
            errNum=0;
            errDesc="";
       }   public boolean isValidUser()
       {
            strSql="select * from `users` ";
    strSql=strSql + " where userName='" + userName + "'";
    strSql=strSql + " and userPassword='"+ userPassword + "'";
    strSql=strSql + " and systemRole= '"+systemRole+"'";
            Statement stmt=null;
            ResultSet rs=null;
            try
            {
        stmt=dbConn.createStatement();
    rs =stmt.executeQuery(strSql);
                if (rs.next())
                {
                    ID=rs.getLong("ID");
                    userName=rs.getString("userName");
                    userPassword=rs.getString("userPassword");               
                    name=rs.getString("name");
                    company=rs.getString("company");                
                    depart=rs.getString("depart");
                    email=rs.getString("email");
                    myfields=rs.getString("myfields");
                    systemRole=rs.getString("systemRole");
                    createDatetime=rs.getString("createDatetime");                
                    tag=rs.getInt("tag");

                    this.errNum=0;
                    this.errDesc="";
                    return true;
                }
                else
                {
                    this.errNum=-1;
                    this.errDesc="Not Valid User";
                    return false;            } }
    catch(Exception ex)
    {
                this.errNum=-1;
                this.errDesc=ex.toString();
                return false; }
            finally
            {        }   }   public void  setConnection(Connection dbConn)
       {
        this.dbConn=dbConn;
       }
       
       private void exeSql(String strSql)
       {
        try
    {
        Statement stmt=dbConn.createStatement();
    stmt.executeUpdate(strSql);
                this.errNum=0;
                this.errDesc="";
    }
    catch(Exception ex)
    {
                this.errNum=-1;
                this.errDesc=ex.toString(); }
       }    public int getErrNum()
        {
            return errNum;    }
       
        public String getErrDesc()
        {
            return errDesc;
        }
    }
      

  4.   

    你用客户端连接一下看看是否有问题,
    或者你把mysql停了,看看3306端口是否有使用?
      

  5.   

    userpackage com.am;import java.util.*;
    import java.sql.*;
    import java.text.*;
    public class user
    {    public long ID;
        public String userName;
        public String userPassword;
        public String name;    
        public String company;   
        public String depart;
        public String email;  
        public String myfields;
        public String systemRole;
        public String createDatetime;   
        public int tag;
        private String strSql;
        private Connection dbConn;
        private int errNum;
        private String errDesc;
        private SimpleDateFormat dateFormatter;
        public user()
        {
            dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ID=0;
            userName="";
            userPassword="";        
            name="";        
            company="";        
            depart="";
            email="";       
            myfields="";       
            systemRole="";
          createDatetime=dateFormatter.format(new java.util.Date());    
    tag=0;
        strSql="";
            dbConn=null;
            errNum=0;
            errDesc="";
       }
      
       public void add()
       {
            String tmpcreateDatetime;
            createDatetime=dateFormatter.format(new java.util.Date());
            strSql="insert into users ";
            strSql=strSql + "(";
            strSql=strSql + "userName,";
            strSql=strSql + "userPassword,";        
            strSql=strSql + "name,";    
            strSql=strSql + "company,";     
            strSql=strSql + "depart,";
            strSql=strSql + "email,";       
            strSql=strSql + "myfields,";
            strSql=strSql + "systemRole,";
            strSql=strSql + "createDatetime,";     
            strSql=strSql + "tag";        
            strSql=strSql + ") ";
    strSql=strSql + "values(";
    strSql=strSql + "'" + userName + "',";
    strSql=strSql + "'" + userPassword + "',";
    strSql=strSql + "'" + name + "',";
    strSql=strSql + "'" + company + "',";
    strSql=strSql + "'" + depart + "',";
    strSql=strSql + "'" + email + "',";
    strSql=strSql + "'" + myfields + "',";
    strSql=strSql + "'" + systemRole + "',";
    strSql=strSql + "'" + createDatetime + "',";
    strSql=strSql + "'" + tag + "'";           
    strSql=strSql + ")";
    exeSql(strSql);
       } 
       public void update()
       {
        strSql="update users set ";
    strSql=strSql + "userName=" + "'" + userName + "',";
    strSql=strSql + "userPassword=" + "'" + userPassword + "',";
    strSql=strSql + "name=" + "'" + name + "',";
    strSql=strSql + "company=" + "'" + company + "',";
    strSql=strSql + "depart=" + "'" + depart + "',";
    strSql=strSql + "email=" + "'" + email + "',";
    strSql=strSql + "myfields=" + "'" + myfields + "',";
    strSql=strSql + "systemRole=" + "'" + systemRole + "',";
    strSql=strSql + "createDatetime=" + "'" + createDatetime + "',";
    strSql=strSql + "tag=" + "'" + tag + "'";
    strSql=strSql + " where ID='" + ID + "'";
    exeSql(strSql);   }
       
          public void updatekey()
          {
           strSql="update users set ";
           strSql=strSql + "userPassword=" + "'" + userPassword + "'";
           strSql=strSql + " where ID='" + ID + "'";
           exeSql(strSql);      }  
       public void delete()
       {
            strSql="delete from `users` where ID='";
            strSql=strSql + this.ID + "'";
            exeSql(strSql);   } 
       public void enable()
       {
            strSql="update users set tag=0 where ID='";
            strSql=strSql  + this.ID + "'";
            exeSql(strSql);   }   
       public void  disable()
       {
            strSql="update users set tag=-1 where ID='";
            strSql=strSql + this.ID + "'";
            exeSql(strSql);   }  
       public boolean  init()
       {
         strSql="select * from `users` where ID=";
            strSql=strSql + "'" + this.ID + "'";
            Statement stmt=null;
            ResultSet rs=null;
            try
    {
        stmt=dbConn.createStatement();
    rs =stmt.executeQuery(strSql);
                if (rs.next())
                {
                    ID=rs.getLong("ID");
                    userName=rs.getString("userName");
                    userPassword=rs.getString("userPassword");               
                    name=rs.getString("name");               
                    company=rs.getString("company");               
                    depart=rs.getString("depart");
                    email=rs.getString("email");                
                    myfields=rs.getString("myfields");
                    systemRole=rs.getString("systemRole");
                    createDatetime=rs.getString("createDatetime");                
                    tag=rs.getInt("tag");               
                    this.errNum=0;
                    this.errDesc="";
                    return true;
                }
                else
                {
                    this.errNum=-1;
                    this.errDesc="init faild!";
                    return false;            } }
    catch(Exception ex)
    {
                this.errNum=-1;
                this.errDesc=ex.toString();
                return false; }
            finally
            {
                stmt=null;
                rs=null;
            }
       }
       public void  setConnection(Connection dbConn)
       {
        this.dbConn=dbConn;
       }
      
       private void exeSql(String strSql)
       {
        try
    {
        Statement stmt=dbConn.createStatement();
    stmt.executeUpdate(strSql);
                this.errNum=0;
                this.errDesc="";
    }
    catch(Exception ex)
    {            this.errNum=-1;
                this.errDesc=ex.toString(); }
       }  
        public int getErrNum()
        {
            return errNum;    }
       
        public String getErrDesc()
        {
            return errDesc;
        }
    }
      

  6.   

    to yangxiao_jiang(哈哈) 将服务停掉就出现下面的错误信息java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is ther
    e a MySQL server running on the machine/port you are trying to connect to? (java
    .net.ConnectException)客户端测试连接我不会,
      

  7.   

    客户端连接我已测过,出现下面的错误信息
    A
     5.0.27-community-nt#BdQHSIV,?QuwxsZG5sf'+遗失对主机的连接。