java中判断mysql数据库是否可以连接

siyue_qi 2008-05-16 04:21:45
看别的朋友用ping()命令判断是否可以连接上数据库,能否不吝赐教。
...全文
1094 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
siyue_qi 2008-05-20
  • 打赏
  • 举报
回复
楼上是教我获得输入的一个IP地址啊,我明白。
怎么在程序了ping服务器?
有劳了。
aoyihuashao1 2008-05-20
  • 打赏
  • 举报
回复


import java.net.*;

public class InetAddressTest
{ public static void main(String[] args)
{ try
{
if (args.length > 0)
{ String host = args[0];
InetAddress[] addresses
= InetAddress.getAllByName(host);
for (int i = 0; i < addresses.length; i++)
System.out.println(addresses[i]);
}
else
{ InetAddress localHostAddress
= InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
}
catch (Exception e)
{ System.out.println("Error: " + e);
}
}
}



siyue_qi 2008-05-16
  • 打赏
  • 举报
回复
只有一个con.isClosed()
不过是判断连接是否关闭啊
siyue_qi 2008-05-16
  • 打赏
  • 举报
回复
是判断是否连接的么,试试先。
aoyihuashao1 2008-05-16
  • 打赏
  • 举报
回复
有这个方法的吧。。。。。

con.is...

记不清了。
siyue_qi 2008-05-16
  • 打赏
  • 举报
回复
判断异常很慢的,我看有朋友说用ping()方法的,可我不会用啊,呵呵。
forestking_xx 2008-05-16
  • 打赏
  • 举报
回复
可以用exception判断吧,如果连接失败,应该会有exception抛出吧。
siyue_qi 2008-05-16
  • 打赏
  • 举报
回复
如果服务器启动了,但数据库服务没有启动,即使PING通了也连不通吧

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧