4 JDBC工具类(将步骤简化)

1.工具类应包含
(1)获取con对象

 public  static Connection getCon() throws SQLException {
        return DriverManager.getConnection(url,name,pass);
    }

(2)关闭资源

public  static  void close(Statement statement,Connection connection){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
    public  static  void close(Statement statement, Connection connection, ResultSet resultSet){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }

2.使用到的技术
(1)路径查找

(2)资源配置

//创造配置集
            Properties properties = new Properties();
            //获取配置路径
            //获取类加载器
            ClassLoader classLoader = JDBCutil.class.getClassLoader();
            URL resource = classLoader.getResource("JDBC.properties");
            String path = resource.getPath();
            System.out.println(path);

            //将配置文件加载进结果集使用
            properties.load(new FileReader(path));
            //开始取配置
            url= properties.getProperty("url");
            name=properties.getProperty("name");
            pass=properties.getProperty("pass");
            driver=properties.getProperty("driver");

3.总的代码

public class JDBCutil {
    private static String url;
    private static String name;
    private static String pass;
    private static String driver;
    static {


        try {
            //创造配置集
            Properties properties = new Properties();
            //获取配置路径
            //获取类加载器
            ClassLoader classLoader = JDBCutil.class.getClassLoader();
            URL resource = classLoader.getResource("JDBC.properties");
            String path = resource.getPath();
            System.out.println(path);

            //将配置文件加载进结果集使用
            properties.load(new FileReader(path));
            //开始取配置
            url= properties.getProperty("url");
            name=properties.getProperty("name");
            pass=properties.getProperty("pass");
            driver=properties.getProperty("driver");
            //开始使用
            //先注册一个
            Class.forName(driver);

        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public  static Connection getCon() throws SQLException {
        return DriverManager.getConnection(url,name,pass);
    }
    public  static  void close(Statement statement,Connection connection){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
    public  static  void close(Statement statement, Connection connection, ResultSet resultSet){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}


6.实际使用时的替代
(1)

connection= JDBCutil.getCon();

(2)

finally {
            JDBCutil.close(statement,connection,resultSet);

        }

5.结果

编号:   2   姓名:   jun
编号:   3   姓名:   feng
编号:   4   姓名:   niu
编号:   1   姓名:   jiang
编号:   5   姓名:   jiang1
编号:   6   姓名:   jiang2
编号:   7   姓名:   jiang3
编号:   8   姓名:   jiang4
编号:   9   姓名:   jiang5
编号:   10   姓名:   jiang5

Process finished with exit code 0

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值