JDBC工具類(druid)

package cn.itcast.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * public static DataSource getDataSource()
 public static Connection getConnection()
 public static void close(Connection conn, Statement stmt, ResultSet rs)

 */

public class JdbcUtils{
    //獲取連接池?cái)?shù)據(jù)源

    public static DataSource getDataSource() throws IOException {
        Properties properties = new Properties();
         //使用相對(duì)路徑加載配置文件,相對(duì)路徑的獲取方法寫在下面的注釋里
        properties.load(new FileInputStream("spring_day02\\src\\main\\resources\\druid.properties"));
        DataSource dataSource = null;
        try {
            //利用屬性創(chuàng)建數(shù)據(jù)源對(duì)象
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
    //獲取連接對(duì)象
    public static Connection getConnection(){
        Connection conn = null ;
        try {
            conn = getDataSource().getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return conn;
    }

    //關(guān)閉資源
    public static void close(Connection conn, Statement stmt, ResultSet rs){
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
//    //測(cè)試連接是否正確
//    public static void main(String[] args) throws IOException {
//      System.out.println("當(dāng)前相對(duì)路徑===="+System.getProperty("user.dir"));
//        System.out.println(JdbcUtils.getConnection());
//    }

    /**
     * druid.proterties
     *       #數(shù)據(jù)庫(kù)連接參數(shù)
             url=jdbc:mysql:///spring01
             username=root
             password=123
             driverClassName=com.mysql.jdbc.Driver
             #連接池連接參數(shù)
             initialSize=3
             maxActive=10
             maxWait=2000
     * pom依賴:
     * <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     </dependency>

     <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     </dependency>
     <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     </dependency>

     <!-- 連接池 -->
     <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     </dependency>
     *
     *
     *
     */
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容