public static Connection JDBC() throws SQLException { Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost_3306:3306/test"; Properties info = new Properties(); info.put("user", "root"); info.put("password", "jfy10122003"); Connection connect = driver.connect(url, info); return connect; }检查代码并修改
时间: 2024-03-11 17:50:27 浏览: 108
在中,localhost_3306应该改为localhost:3306,代码修改如下:
```
public static Connection JDBC() throws SQLException {
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "jfy10122003");
Connection connect = driver.connect(url, info);
return connect;
}
```
此外,建议在使用完Connection后及时关闭连接,避免资源泄露。可以在finally块中关闭连接,代码修改如下:
```
public static Connection JDBC() throws SQLException {
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "jfy10122003");
Connection connect = null;
try {
connect = driver.connect(url, info);
// ... do something with the connection
} finally {
if (connect != null) {
connect.close();
}
}
return connect;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)