如何将 try-with-resources 与 JDBC 一起使用?
每当我们实例化和使用某些对象/资源时,我们都应显式关闭它们,否则有 资源泄漏 的可能性。
通常,我们使用 finally 块来关闭资源,如下所示 −
Connection con = null; Statement stmt = null; ResultSet rs = null; //Registering the Driver try { con = DriverManager.getConnection(mysqlUrl, "root", "password"); stmt = con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } finally { try { rs.close(); stmt.close(); con.close(); } catch(SQLException e) { e.printStackTrace(); } }
自 JSE7 起,引入了 try-with-resources 语句。其中,我们在 try 块中声明一个或多个资源,它们将在使用后自动关闭。(在 try 块的末尾)
我们在 try 块中声明的资源应扩展 java.lang.AutoCloseable 类。
在 JDBC 中,我们可以在 try-with-resources 语句中使用 java.sql.CallableStatement、Connection、PreparedStatement、Statement、ResultSet 和RowSet。
范例
让我们使用 CREATE 语句在 MySQL 数据库中创建名为 MyPlayers 的表,如下所示 −
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用 INSERT 语句在 MyPlayers 表中插入 7 条记录 −
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
以下 JDBC 程序演示了如何在 JDBC 中使用 try-with-resources 语句 −
范例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TryWithResources_Example { public static void main(String args[]) { //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; System.out.println("Connection established......"); //Registering the Driver try(Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); Statement stmt = con.createStatement(); ) { try(ResultSet rs = stmt.executeQuery("select * from MyPlayers");){ //Retrieving the data while(rs.next()) { System.out.print(rs.getInt("ID")+", "); System.out.print(rs.getString("First_Name")+", "); System.out.print(rs.getString("Last_Name")+", "); System.out.print(rs.getDate("Date_Of_Birth")+", "); System.out.print(rs.getString("Place_Of_Birth")+", "); System.out.print(rs.getString("Country")); System.out.println(); } } catch (SQLException e) { e.printStackTrace(); } } catch (SQLException e) { e.printStackTrace(); } } }
输出
Connection established...... 1, Shikhar, Dhawan, 1981-12-05, Delhi, India 2, Jonathan, Trott, 1981-04-22, CapeTown, SouthAfrica 3, Kumara, Sangakkara, 1977-10-27, Matale, Srilanka 4, Virat, Kohli, 1988-11-05, Mumbai, India 5, Rohit, Sharma, 1987-04-30, Nagpur, India 6, Ravindra, Jadeja, 1988-12-06, Nagpur, India 7, James, Anderson, 1982-06-30, Burnely, England 8, Ryan, McLaren, 1983-02-09, Kumberly, null
广告