手工释放mysql 数据库链接

在使用mysql数据库时,经常需要手工释放数据库链接。

try{ //1、加载驱动类 Class.forName("com.mysql.jdbc.Driver"); //2、建立数据库链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "root"; Connection conn = DriverManager.getConnection(url, username, password); // 3、使用数据库链接 (这里省略具体操作) }catch(Exception e){ e.printStackTrace(); }finally{ //4、手工释放链接 if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }