JDBCUtils中的配置文件加载不当而引发空指针的问题

本文深入探讨了在使用JDBCUtils类进行数据库连接池配置时,因配置文件加载不当而导致的空指针异常问题。通过具体代码示例,详细解析了正确加载配置文件druid.properties的方法,确保连接池正常初始化,避免运行时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JDBCUtils中的配置文件加载不当而引发空指针的问题

代码

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
	1. 声明静态数据源成员变量
	2. 创建连接池对象
	3. 定义公有的得到数据源的方法
	4. 定义得到连接对象的方法
	5. 定义关闭资源的方法
 */
public class JDBCUtils {
	// 1.	声明静态数据源成员变量
	private static DataSource ds;

	// 2. 创建连接池对象
	static {
		// 加载配置文件中的数据
		InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
		Properties pp = new Properties();
		try {
			pp.load(is);
			// 创建连接池,使用配置文件中的参数
			ds = DruidDataSourceFactory.createDataSource(pp);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 3. 定义公有的得到数据源的方法
	public static DataSource getDataSource() {
		return ds;
	}

	// 4. 定义得到连接对象的方法
	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}

	// 5.定义关闭资源的方法
	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {}
		}

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

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

	// 6.重载关闭方法
	public static void close(Connection conn, Statement stmt) {
		close(conn, stmt, null);
	}
}

注意,问题在于这句

// 加载配置文件中的数据
		InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");

配置文件"druid.properties"和JDBCUtils类都是在同一个工程下,就不要写成"/druid.properties",不然无法加载该配置文件,也就无法创建数据库连接池,引发一连串的空指针异常

public class JdbcMytest { static DataSource dataSource; public static void main(String[] args) throws Exception { //DataSource dataSource = new DruidDataSource(); JdbcMytest demo = new JdbcMytest(); demo.test(); //Class.forName("com.mysql.cj.jdbc.Driver"); //Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "123456"); Connection connection = JdbcMytest.getConnection(); String sql = "select e.did,e.ename,e.gender,e.salary from t_employee e join t_department d on e.did = d.did where salary >8000 group by e.gender "; PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()){ int anInt = resultSet.getInt(1); Object ename = resultSet.getObject("ename"); String gender = resultSet.getString("gender"); int anInt1 = resultSet.getInt(4); System.out.println(anInt+"\t"+ename+"\t"+gender+"\t"+anInt1); } resultSet.close(); preparedStatement.close(); connection.close(); } /*创建连接池,使用连接池去拿连接对象连接数据库,就不用每次创建连接对象避免内存开销*/ public void test(){ InputStream resourceAsStream = JdbcUtils.class.getClassLoader().getResourceAsStream("properties"); Properties properties = new Properties(); try { properties.load(resourceAsStream); } catch (IOException e) { throw new RuntimeException(e); } try { dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { throw new RuntimeException(e); } } public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } }
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值