获取不到BasicDataSource对象空指针
DBCP加载数据库连接,参考老师的写,还是获取不到BasicDataSource对象啊。一直报空指针
依赖文件
文件路径 resources/dbcp.properties
dbcp.properties 配置里面
===================================================================
driverClassName=dm.jdbc.driver.DmDriver url=jdbc:mysql://127.0.0.1:3306/imooc_db?userUnicode=true&characterEncoidng = UFT-8 username=root password= minIdle=5 mzxIdle=20 maxActive=400 initialSize=30 logAbandoned=true removeAbandoned=true maxWait=5000
==========================================================================
/** * 数据源,static */ private static BasicDataSource DS; //配置文件位置 private static final String cofigFile = "resources/dbcp.properties";
// 构造方法
public void DBCPUtils() { initDbcp(); }
//dbcp初始化代码
private void initDbcp() { Properties props = new Properties(); try { //以流形式读取配置文件 File file = new File(cofigFile); FileInputStream in = new FileInputStream(file); props.load(in); //得到数据源 DS = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
-- 获得数据库连接
//获得数据库连接
public Connection getConn() { Connection conn = null; try { if(null != DS) { conn = DS.getConnection(); //设置成手动提交事务 conn.setAutoCommit(false); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; }
===================================================
现在想要获取BasicDataSource,可是一直就报错NullPointExcepint ........