2011-03-07 28 views
0

嘿家伙,我在我的主机上运行一个MySQL服务器。 下面提供了代码。当我尝试从另一台连接到相同路由器的机器运行相同的代码时。我得到了我在代码下面提供的异常。嘿需要帮助与MySQL异常我越来越abt通信链接失败

请帮忙。代码: import java.sql。 ; import java.io.;

public class Login{ 
public static void main(String[] args) throws IOException { 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
Connection conn = null; 
String url = "jdbc:mysql://"+br.readLine()+":3306/"; 
String dbName = "p2p"; 
String driver = "com.mysql.jdbc.Driver"; 
String userName = "root"; 
String password = "123"; 
System.out.println("Please input Username:"); 
String user=br.readLine(); 
System.out.println("Please input Password:"); 
String pass=br.readLine(); 

try { 
    Class.forName(driver).newInstance(); 
    conn = DriverManager.getConnection(url+dbName,userName,password); 
    String sql="select password from newuser where username='"+user+"'"; 
    Statement stmt=null; 
    ResultSet rs=null; 

    try{ 
     stmt=conn.createStatement(); 
     rs=stmt.executeQuery(sql); 
    if(rs.next()) 
    { 
     if(rs.getString("password").equals(pass)) 
      System.out.println("true"); 
     else 
      System.out.println("false"); 
    } 
    else System.out.println("false"); 
    } 
    catch(Exception e){System.out.println(e);} 
    conn.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
} 

}}

例外: -

com.mysql.jdbc.CommunicationsException: Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The driv 
er has not received any packets from the server. 
     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1 
112) 
     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:346) 
     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2334) 
     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2 
371) 
     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163) 
     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794) 
     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:374) 
     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java 
:305) 
     at java.sql.DriverManager.getConnection(DriverManager.java:525) 
     at java.sql.DriverManager.getConnection(DriverManager.java:171) 
     at Login.main(login.java:20) 
Caused by: java.net.UnknownHostException: 192.168.1.23306: 192.168.1.23306 
     at java.net.InetAddress.getAllByName0(InetAddress.java:1128) 
     at java.net.InetAddress.getAllByName0(InetAddress.java:1098) 
     at java.net.InetAddress.getAllByName(InetAddress.java:1061) 
     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.ja 
va:244) 
     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:295) 
     ... 9 more 

回答

0

在同一网络上的机器或允许在同一个VLAN。你得到的错误意味着程序甚至无法看到MySQL服务器。另一种可能性,请检查MySQL上的帐户,并确保两台服务器都在允许的帐户HOSTS中。

+0

我更新了mysql服务器中的权限表以允许特定用户登录。 – anonymous123 2011-03-07 08:52:04

+0

这是否有效?其他人可能会得到这个错误,并可能想知道如何解决它。 – ProNeticas 2011-03-09 02:11:31

0

你的堆栈跟踪有这样一行:

java.sql.DriverManager.getConnection(DriverManager.java:171) at Login.main(login.java:20) Caused by: java.net.UnknownHostException: 192.168.1.23306: 192.168.1.23306 at 

这意味着你的Java进程不知道主机地址192.168.1.23306

+0

mysql服务器在192.168.1.2上 – anonymous123 2011-03-07 08:51:25

+0

那么堆栈跟踪说192.168.1.23306的未知主机。这意味着要么在192.168.1.2:3306上没有运行MySQL,要么在从System.in读取主机名时出现问题。你可以改变这一行的网址为: String url =“jdbc:mysql://192.168.1.2:3306 /”; 并尝试运行您的代码。 – anubhava 2011-03-07 14:22:28

0

嗨,我得到了错误。 而不是使用br.readLine()当我把mysql服务器m/c的实际IP程序运行。

+0

在这种情况下,请您将我的答案标记为“已接受”?如果你看到我的答案和评论,我确实建议你,即改变你的网址为String url =“jdbc:mysql://192.168.1.2:3306 /”;在你的代码中。 – anubhava 2011-03-16 04:12:15

相关问题