在上一篇文章Druid-建立Druid鏈接池中,咱們已經建立好了Druid鏈接池,也就是建立好Druid工具類sql
接下來咱們就使用該工具類執行SQL語句,測試該工具類是否能夠正常工具數據庫
本文使用的數據是segmentfault
/** * 項目描述: 使用自定義編寫的數據庫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); } } }
返回正確結果工具