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 @   我也有梦想呀  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示