JDBC JDBCUtil工具类(idea 封装)

jdbc引言

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,
	可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
	JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,
	同时,JDBC也是个商标名。

jdbc工具类的封装

package com.baizhi.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/* 描述:这个工具类是oracle工具类的封装 思路:通过输出流将jdbc.properties文件读入到这个文件中,将这个文件进行线程绑定, 使同一个事物使用同一个连接对象,保证了资源的不浪费性 */
public class JdbcUtil {
	//输出流
    private static InputStream inputStream = JdbcUtil.class.getResourceAsStream("/jdbc.properties");
    //配置文件
    private static Properties properties = new Properties();
    //线程
    private static ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    //加载驱动 创建连接
    public static Connection getConnection() throws ClassNotFoundException, IOException, SQLException {
        properties.load(inputStream);
        Class.forName(properties.getProperty("oracle.driver"));
        Connection connection = connectionThreadLocal.get();
        if (connection == null) {
            connection = DriverManager.getConnection(properties.getProperty("oracle.url"), properties.getProperty("oracle.username"), properties.getProperty("oracle.password"));
            connectionThreadLocal.set(connection);
        }
        return connection;
    }

    public static void close(PreparedStatement preparedStatement, Connection connection, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
                connectionThreadLocal.remove();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void close(PreparedStatement preparedStatement, ResultSet resultSet) {

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement preparedStatement) {

        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
                connectionThreadLocal.remove();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
jdbc.proties
oracle.driver=oracle.jdbc.OracleDriver
	oracle.url=jdbc:oracle:thin:@localhost:1521:xe
	oracle.username=hr
	oracle.password=hr
工具类测试
import com.baizhi.util.JdbcUtil;
import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

public class JdbcUtilTest {
    @Test
    public void test1(){
        try {
            Connection connection = JdbcUtil.getConnection();
            System.out.println("connection = " + connection);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

idea 目录结构

idea web项目结构

在这里插入图片描述

感谢大家的观看,同时希望大家可以踊跃提一些建议,用于技术交流。由于是刚开始写博客可能写的不太好,
	希望大家理解
  • idea web 项目的创建
  • 详解