在上一篇文章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