在上一篇文章C3P0-基于mysql8创立C3P0连接池(jdbcUrl写法)中, 咱们编写好了C3P0的配置文件, 并且自定义工具类C3P0Utils
.
当初就通过调用该工具类中的办法的形式执行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(); } } }}
运行返回后果正确