JDBCUtils工具类

package com.qbb.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**

  • @author QiuQiu&LL (个人博客:www.qiuit.xyz)

  • @version 1.0

  • @Description:
    */
    public class JDBCUtils {
    private static final String DRIVERCLASS;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    static {
    InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
    //创建配置对象
    Properties properties = new Properties();
    // 将配置文件和配置对象绑定起来
    try {
    properties.load(inputStream);
    } catch (IOException e) {
    e.printStackTrace();
    }
    DRIVERCLASS = properties.getProperty("driverClass");
    URL = properties.getProperty("jdbcUrl");
    USERNAME = properties.getProperty("username");
    PASSWORD = properties.getProperty("password");
    }

    private static void loadDriver() throws ClassNotFoundException {
    Class.forName(DRIVERCLASS);
    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
    loadDriver();
    return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    /**

    • 释放资源
    • @param connection
    • @param statement
    • @param resultSet
      */
      public static void release(Connection connection, Statement statement, ResultSet resultSet) {
      if (null != connection) {
      try {
      connection.close();
      } catch (SQLException throwables) {
      throwables.printStackTrace();
      }
      connection = null;
      }
      if (null != statement) {
      try {
      statement.close();
      } catch (SQLException throwables) {
      throwables.printStackTrace();
      }
      statement = null;
      }
      if (null != resultSet) {
      try {
      resultSet.close();
      } catch (SQLException throwables) {
      throwables.printStackTrace();
      }
      resultSet = null;
      }
      }

    /**

    • 释放资源
    • @param connection
    • @param statement
      */
      public static void release(Connection connection, Statement statement) {
      release(connection, statement, null);
      }
      }
posted @ 2022-01-11 16:08  我也有梦想呀  阅读(101)  评论(0)    收藏  举报