为了账号安全,请及时绑定邮箱和手机立即绑定

JDBC DriverManager 在尝试连接到数据库之前检测用户是否在网络上?

JDBC DriverManager 在尝试连接到数据库之前检测用户是否在网络上?

Helenr 2021-12-30 17:35:13
我正在开发一个 Java 应用程序,该应用程序可以连接到我的办公室网络或不连接。我的问题是,当用户未连接时,DriverManager.getConnection()最多需要 20 秒才能最终释放它无法连接到数据库。将我连接或不连接到服务器的代码:public static Connection getConnection() {    try{        //cnx is my Connection object        if(cnx ==null || cnx.isClosed()){            Class.forName("com.mysql.jdbc.Driver");            //Get the connection if possible            cnx = DriverManager.getConnection(connectionString, user, password);        }        /*        Basically, I'm just checking if "ConfigUser.isConnected()" is true        later in the code to know if I should try to execute SQL queries        */        ConfigUser.setConnected(true);        return cnx;    }    catch (Exception e){    }    ConfigUser.setConnected(false);    return null;}我尝试使用该DriverManager.setLoginTimeout()功能,但它似乎没有任何改变。
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

添加以connectionString 毫秒为单位的连接超时

&connectTimeout=5000

连接超时

套接字连接超时(以毫秒为单位),0 表示没有超时。仅适用于 JDK-1.4 或更新版本。默认为“0”。


查看完整回答
反对 回复 2021-12-30
  • 1 回答
  • 0 关注
  • 248 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号