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;
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项目结构

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