[英]ssh Exception in Java: java.net.ConnectException: Connection refused: connect
[英]How to catch a “java.net.ConnectException: Connection refused: connect” exception
为了检查服务器关闭或网络不正常时会发生什么,我停止了Apache和MySQL服务并运行了我的代码。
我收到以下错误:
java.net.ConnectException:连接被拒绝:connect
如何在代码中捕获此异常?
我已经试过了:
public static Connection getCon(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
}
catch(Exception e){
System.out.println(e.getMessage());
if(e.getCause() instanceof SQLException){
JOptionPane.showMessageDialog(null, "Connection refused!");
}
}
return con;
}
还有这个
public static Connection getCon(){
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/zomatocrm","root","");
}
catch(Exception e){
System.out.println(e.getMessage());
if(e.getCause() instanceof ConnectException){
JOptionPane.showMessageDialog(null, "Connection refused!");
}
}
return con;
}
另外,我已经使用该连接与xampp localhost服务器进行数据交换,然后尝试停止xamp并仍然收到上述异常。
如何让我的代码完全捕获异常?
考虑到这一点:
public static Connection getCon() {
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/zomatocrm", "root", "");
}
catch (ConnectException e){
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, "Connection refused!!!");
}
return con;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.