package com.template.util;



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

/**
 * @Author：liuxinyue
 * @Package：com.template.util
 * @Project：cloud-server
 * @name：IOTdbJDBCUtils
 * @Date：2024/9/24 10:18
 */
public class IOTdbJDBCUtils {
    private static final String driver = "org.apache.iotdb.jdbc.IoTDBDriver";
    private final String url;
    private final String username;
    private final String password;

    public IOTdbJDBCUtils(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("当前加载的驱动不存在........,请检查后重试!");
        }
    }

    public Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
