Druid-测试Druid工具类

在上一篇文章Druid-建立Druid链接池中,咱们已经建立好了Druid链接池,也就是建立好Druid工具类sql

接下来咱们就使用该工具类执行SQL语句,测试该工具类是否能够正常工具数据库

本文使用的数据是
image.pngsegmentfault

/**
 *   项目描述: 使用自定义编写的数据库Druid链接池的工具类获取链接进行sql查询操做
 *   做   者: chain.xx.wdm
 *   备   注:
 */

public class DruidPoolTest {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 1.获取链接
            connection = DruidUtils.getConnection();
            // 2.获取Statement对象,执行sql查询
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select name from employee where salary between 3000 and 5000");
            // 3.处理结果集
            while(resultSet.next()){
                String ename = resultSet.getString("name");
                System.out.println(ename);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4.关闭对象
            DruidUtils.close(connection, statement, resultSet);
        }
    }
}

image.png

返回正确结果工具

相关文章
相关标签/搜索