Druid工具类

Druid工具类

定义Druid工具类的步骤

  1. 定义一个Druid类
  2. 提供静态代码块加载配置文件,初始化连接池对象
  3. 提供方法
    1. 获取连接方法(getConnection):通过数据库连接池获取连接
    2. 释放资源
    3. 获取连接池的方法(DataSource)
package com.haikang.druid;

/**
 * Druid工具类
 * 1. 定义一个Druid类
 * 2. 提供静态代码块加载配置文件,初始化连接池对象
 * 3. 提供方法
 *    1. 获取连接方法(getConnection):通过数据库连接池获取连接
 *    2. 释放资源
 *    3. 获取连接池的方法(DataSource)
 */
//1. 定义一个Druid类
public class DruidUtils {
    //定义DataSource变量
    private static DataSource dataSource = null;

    //2. 提供静态代码块加载配置文件,初始化连接池对象
    static {

        try {
            //加载配置文件druid.properties
            Properties properties = new Properties();
            properties.load(DebugUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//加载文件
            //获得数据库连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 1. 获取连接方法(getConnection):通过数据库连接池获取连接
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /**
     *  2. 释放资源
     */
    public static void close(Statement statement,Connection connection){
//        if (statement!=null){
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if (connection!=null){
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
        close(null,statement,connection);
    }

    public static void close(ResultSet resultSet,Statement statement,Connection connection){

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

    /**
     * 获取数据库连接池对象DataSource
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
}
package com.haikang;

/**
 * DruidUtils工具类的测试
 * 向account表插入一条记录
 */
public class DruidUtilsTest {
    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            //获取数据库连接对象
            connection = DruidUtils.getConnection();
            //定义sql语句
            String sql = "insert into account values (null,?,?)";
            //获取Sql执行对象
            preparedStatement = connection.prepareStatement(sql);
            //给占位符赋值
            preparedStatement.setString(1,"南宁");
            preparedStatement.setDouble(2,4988);
            //执行Sql
            int i = preparedStatement.executeUpdate();
            //处理返回结果
            System.out.println(i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //关流
        finally {
            DruidUtils.close(preparedStatement,connection);
        }
    }
}
package com.haikang;
/**
 * DruidUtils工具类的测试
 * 查询account表的所有记录
 */
public class DruidUtilsTest2 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try {
            //获得数据库连接池
            connection = DruidUtils.getConnection();
            //定义Sql语句
            String sql = "select * from account where id=?";
            //获得Sql执行语句
           preparedStatement = connection.prepareStatement(sql);
            //给占位符赋值
            preparedStatement.setInt(1,1);
            //执行Sql语句
            resultSet = preparedStatement.executeQuery();
            //处理结果
            while (resultSet.next()!=false){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                double aDouble = resultSet.getDouble(3);

                System.out.println(id+"\t"+name+"\t"+aDouble);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            DruidUtils.close(resultSet,preparedStatement,connection);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值