java如何关闭数据库资源,如何在程序结束时关闭数据库连接?

In a Java program, I have a singleton class that holds the database connection, which is used by the entire program.

How do I tell Java to close the connection when the program ends?

I can put a connection.close() statement at the end of main, but this will not be executed if the program ends unexpectedly, e.g, by an uncaught exception or "exit" call somewhere within the program. How do I tell Java to close the connection regardless of how the program ends?

解决方案

You could use a VM shutdown hook if the method System.exit might be called.

Runtime.getRuntime().addShutdownHook(new Thread(() -> closeDatabaseConnection()));

Shutdown hooks also will be called if the program exits normally:

The Java virtual machine shuts down in response to two kinds of events:

The program exits normally, when the last non-daemon thread exits or when the System.exit method is invoked, or

The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or system shutdown.

But there is no guarantee they will be run if something unexpected happens:

In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java连接MySQL数据库的基本步骤如下: 1. 加载MySQL JDBC驱动程序:在连接MySQL数据库之前,需要加载MySQL JDBC驱动程序,可以使用Class.forName()方法加载驱动程序,如下所示: ```java Class.forName("com.mysql.jdbc.Driver"); ``` 2. 建立数据库连接:使用DriverManager.getConnection()方法连接MySQL数据库,需要提供连接字符串、用户名和密码,如下所示: ```java String url = "jdbc:mysql://hostname:port/database"; String username = "username"; String password = "password"; Connection conn = DriverManager.getConnection(url, username, password); ``` 其中,url是MySQL数据库的连接字符串,包括主机名、端口号、数据库名等信息。 3. 创建Statement对象:使用Connection.createStatement()方法创建Statement对象,用于执行SQL语句,如下所示: ```java Statement stmt = conn.createStatement(); ``` 4. 执行SQL语句:使用Statement.executeUpdate()方法执行INSERT、UPDATE、DELETE等SQL语句,使用Statement.executeQuery()方法执行SELECT语句,如下所示: ```java String sql = "SELECT * FROM table_name"; ResultSet rs = stmt.executeQuery(sql); ``` 5. 处理查询结果:使用ResultSet对象处理查询结果,如下所示: ```java while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); // 处理查询结果 } ``` 6. 关闭数据库连接:在程序结束,需要关闭数据库连接,释放资源,如下所示: ```java rs.close(); stmt.close(); conn.close(); ``` 以上就是Java连接MySQL数据库的基本步骤。注意,在实际开发中,需要处理异常、使用PreparedStatement等更加安全和高效的方式执行SQL语句。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值