|NO.Z.00082|——————————|BigDataEnd|——|Java&MySQL.JDBC.V07|——|MySQL.v07|Jdbc开发_释放资源|

一、API使用:释放资源
### --- API使用:释放资源

~~~     需要释放的对象:ResultSet 结果集,Statement 语句,Connection 连接
~~~     释放原则:先开的后关,后开的先关。ResultSet ==> Statement ==> Connection
~~~     放在哪个代码块中:finally 块
~~~     与IO流一样,使用后的东西都需要关闭!关闭的顺序是先开后关, 先得到的后关闭,后得到的先关闭
二、代码示例
public class JDBCDemo05 {
    
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        
        try {
            //1.注册驱动(省略)
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/db4";
            connection = DriverManager.getConnection(url, "root", "123456");
            
            //3.获取 Statement对象
            statement = connection.createStatement();
            
            String sql = "select * from jdbc_user";
            resultSet = statement.executeQuery(sql);
            
        } catch (SQLException e) {
            
            e.printStackTrace();
            
        } finally {
            /**
* 开启顺序: connection ==> statement => resultSet
* 关闭顺序: resultSet ==> statement ==> connection
*/
            try {
                connection.close();
                resultSet.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
三、步骤总结
### --- 步骤总结

——>        1. 获取驱动(可以省略)
——>        2. 获取连接
——>        3. 获取Statement对象
——>        4. 处理结果集(只在查询时处理)
——>        5. 释放资源
四、sql语句
package com.yanqi.jdbc05;

import java.sql.*;

public class JdbcDemo03 {
    
        public static void main(String[] args) {

            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;

            try {
                //1.注册驱动 省略
                //2.获取连接
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "123456");

                //3.获取语句执行对象
                statement = connection.createStatement();

                //4.执行SQL
                String sql = "select * from jdbc_user";
                resultSet = statement.executeQuery(sql);

                //5.处理结果集对象

            } catch (SQLException e) {
                e.printStackTrace();

            }finally {
                //finally 中的代码始终会执行
                try {
                    resultSet.close();
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
}

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示