嘿我需要帮助mysql异常我得到了通信链接失败

时间:2011-03-07 05:34:44

标签: mysql client-server

嘿伙计们,我在我的主机上运行了一个mysql服务器。 代码如下所示。当我尝试2从连接到同一路由器的另一台机器运行相同的代码时。我得到了我在代码下面提供的异常。

请帮忙。

代码: - 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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

答案 2 :(得分:0)

嘿伙计们,我收到了错误。 当我把mysql服务器m / c的实际ip放到程序运行时,而不是使用br.readLine()。