sqlserver jdbc timeout
时间: 2023-09-30 20:09:58 浏览: 101
sqlserver jdbc
SQL Server JDBC driver provides a property called `loginTimeout` that specifies the number of seconds to wait for a connection to be established before timing out. This property can be set through the JDBC URL or programmatically in your Java code.
To set the `loginTimeout` property through the JDBC URL, you can append it to the end of the connection string as shown below:
```
jdbc:sqlserver://<server>:<port>;databaseName=<database>;loginTimeout=<timeout>
```
Replace `<server>` with the name or IP address of your SQL Server instance, `<port>` with the port number (default is 1433), `<database>` with the name of the database, and `<timeout>` with the number of seconds to wait for a connection to be established before timing out.
Alternatively, you can set the `loginTimeout` property programmatically in your Java code as shown below:
```
String url = "jdbc:sqlserver://<server>:<port>;databaseName=<database>";
Properties props = new Properties();
props.setProperty("user", "<username>");
props.setProperty("password", "<password>");
props.setProperty("loginTimeout", "<timeout>");
Connection conn = DriverManager.getConnection(url, props);
```
Replace `<username>` and `<password>` with the credentials for connecting to the database, and `<timeout>` with the number of seconds to wait for a connection to be established before timing out.
Note that the `loginTimeout` property only affects the initial connection to the database. Once the connection is established, the `queryTimeout` property can be used to set a timeout for individual SQL statements.
阅读全文