2012-07-31 27 views
0

我握手期間收到的錯誤通訊故障。有沒有在本地主機上運行的服務器:3306?我寫了一個jdbc程序,使用類型4的jdbc驅動程序,我想執行刪除操作,IDE是eclipse。 代碼:檢測JDBC - 通信錯誤(MySQL的+的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) 
+0

你得到了什麼錯誤MySQL時,可以ü簡單介紹一下? – 2012-07-31 13:25:55

+0

java.sql.SQLException:握手期間通信失敗。有沒有在本地主機上運行的服務器:3306? \t在com.mysql.jdbc.MysqlIO.init(未知源) – user1450954 2012-07-31 13:36:03

+0

它可能是因爲mysql,你檢查你的mysql在本地主機上運行(端口3306) – 2012-07-31 13:39:28

回答

0

這可能是因爲使用的是舊MySQL Connector/J JDBC驅動的。我假設你已經安裝了最新的MySQL,並且你正嘗試使用老版本的MySQL Connector jar進行連接。

你可能想,如果你編譯從命令提示符Java代碼從MySQL drivers download website

0

更新驅動程序意味着你需要你通過這種方式編譯之前設置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

,也看到該鏈接使用,你需要設置CLASSPATH看到鏈接

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