嘿,需要有关 mysql 异常的帮助,我遇到 abt 通信链接失败
大家好,我正在我的主机上运行 mysql 服务器。 下面提供了代码。当我尝试 2 从连接到同一路由器的另一台计算机运行相同的代码时。我得到了代码下面提供的异常。
请帮忙。
代码:- 导入 java.sql.; 导入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
Hey guys, i m running a mysql server on my host machine.
The code is provided below.When i try 2 run the same code from another machine connected to the same router.I get the exception that I hav provided below the code.
Please help.
Code:-
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();
}
}
}
Exception:-
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
机器是否位于同一网络或允许在同一 VLAN 中。您收到的错误意味着该程序甚至无法看到 MySQL 服务器。另一种可能性是,检查 MySQL 上的帐户并确保两台服务器都位于该帐户允许的主机中。
Are the machines on the same network or allowed in the same VLAN. The error that you are getting means the program cannot even see the MySQL server. Another possibility, check the account on MySQL and make sure that the both servers are in the allowed HOSTS for the account.
您的堆栈跟踪有这一行:
这意味着您的 Java 进程不知道主机地址 192.168.1.23306
Your stack trace has this line:
Which means your Java process doesn't know about host address 192.168.1.23306
嘿伙计们,我收到错误了。
当我将 mysql 服务器的实际 ip 放入程序运行时,而不是使用 br.readLine() 。
Hey guys, i got the error.
Instead of using br.readLine() when i put the actual ip of the mysql server m/c the program runs.