jdbc怎么设置连接超时时间
lewis
2017-12-14
25次阅读
在JDBC中,可以使用`java.sql.Statement`或`java.sql.Connection`对象的`setQueryTimeout`方法来设置连接超时时间。
使用`Statement`对象的`setQueryTimeout`方法:
```java
Statement stmt = connection.createStatement();
stmt.setQueryTimeout(timeoutSeconds);
```
使用`Connection`对象的`setQueryTimeout`方法:
```java
connection.setQueryTimeout(timeoutSeconds);
```
其中,`timeoutSeconds`表示连接超时的时间,以秒为单位。如果在指定的时间内,数据库操作没有完成,将抛出`java.sql.SQLTimeoutException`异常。

发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。