package com.gyee.wisdom.report.config.sqlite;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
public class Database {

    @Value("${spring.datasource.driver-class-name}")
    private String driver;

    @Value("${spring.datasource.url}")
    private String url;

    private Connection connection = null;



    public Connection getConnection() throws Exception {
//        if (connection == null || connection.isClosed()) {
//
//        }
        try{
            Class.forName(driver);
            connection = DriverManager.getConnection(url);
            System.out.println("数据库连接成功");

        }catch (Exception e){
            throw new Exception(e.getMessage());
        }

        return connection;
    }

    //释放资源
    public void releaseResource(){
        if(connection!=null){
            try{
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
