在上一篇文章C3P0-基于mysql8建立C3P0链接池(jdbcUrl写法)中, 咱们编写好了C3P0的配置文件, 而且自定义工具类C3P0Utils
.mysql
如今就经过调用该工具类中的方法的方式执行sql语句,以此测试该工具类能否正常使用(配置文件是否编写正确, 成员方法是否建立成功 等)sql
这里使用的数据表是数据库
/** * 项目描述: 使用自定义编写的数据库C3P0链接池的工具类获取链接进行sql查询操做 * 做 者: chain.xx.wdm */ public class C3P0PoolTest { public static void main(String[] args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { // 1。获取链接 connection = C3P0Utils.getConnection(); // 2。建立有占位符形式的sql查询语句 String sql = "select * from employee where name = ?"; // 3。建立prepareStatement对象,并设置占位符的数值 preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,"猪八戒"); // 4。执行sql语句 resultSet = preparedStatement.executeQuery(); // 5。处理结果集 while(resultSet.next()){ int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String gender = resultSet.getString("gender"); double salary = resultSet.getDouble("salary"); String join_date = resultSet.getString("join_date"); System.out.println("id: " + id + ", name: " + name + ", gender: " + gender + ", salary: " + salary + ", join_date:" + join_date); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { // 6。关闭对象 try { C3P0Utils.close(connection, preparedStatement, resultSet); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
运行返回结果正确segmentfault