JDBC - 通信错误(MySQL + Eclipse)

时间:2012-07-31 13:21:04

标签: jdbc

我收到错误握手期间通信失败。是否在localhost:3306上运行服务器?我已经使用类型4 jdbc驱动程序编写了一个jdbc程序,我想执行删除操作,ide是eclipse。 代码:

Class.forName("com.mysql.jdbc.Driver");
String cs="jdbc:mysql://localhost:3306/account_db";
Connection con=DriverManager.getConnection(cs,"root",<password>);
PreparedStatement ps=con.prepareStatement("delete from empde emp_id=?");
Scanner s=new Scanner(System.in);
while(true)
{
    System.out.println("enter the emp_id :");
    int ac_id=s.nextInt();
    ps.setInt(1,emp_id);
    ps.executeUpdate();
    System.out.println("one emp_id is deleted:");
    System.out.println("one more emp_id?(yes/no)");
    String choice=s.next();
    if(choice.equals("no"))
    System.out.println("empde is exit");
    break;
}
ps.close();
con.close();

检测到错误:

java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306? at com.mysql.jdbc.MysqlIO.init(Unknown Source) 

2 个答案:

答案 0 :(得分:0)

这可能是因为使用了旧的MySQL Connector/J JDBC驱动程序。我假设您安装了最新的MySQL,并且您尝试使用旧版本的MySQL Connector jar进行连接。

您可能希望从MySQL drivers download website

更新您的驱动程序

答案 1 :(得分:0)

如果从cmd提示符编译java代码意味着您需要在编译之前设置java路径。

set path=(drivein which java is stored):\program files\java\jdk(version)\bin;
ex: set path=C:\program files\Java\jdk1.6.0\lib;

设置环境变量意味着您不需要为每次设置java路径,只需查看如何设置环境变量的链接。

http://www.roseindia.net/java/java-classpath.shtml

并且在使用mysql时也看到了链接,你需要设置CLASSPATH,看看链接

http://java.blogs.webucator.com/2010/07/05/java-database-connectivity-jdbc-basics-using-mysql/