2 gjx112266 gjx112266 于 2016.09.18 20:33 提问

关于dbcp连接池显示空指针异常

写了dbcp连接池 提示空指针异常啊,包都有导
图片说明

2个回答

Cincinnati_De
Cincinnati_De   2016.09.18 20:37

能截一下报错的图吗?

Cincinnati_De
Cincinnati_De   2016.09.18 20:58

package com.vdata.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

public class DBCPUtil {
private static DataSource ds;
static{
Properties props = new Properties();
try {
props.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
ds = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}

public static DataSource getDataSource(){
    return ds;
}
public static Connection getConnection() throws SQLException{
    return ds.getConnection();
}

}

package com.vdata.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.vdata.utils.JdbcUtils;

public class JdbcUtilsTest {

@Test
public void test() throws Exception {
    Connection conn = JdbcUtils.getConnection();
    // 3、创建与数据库交互的Statement对象
    Statement stat = conn.createStatement();
    // 4、执行SQL语句
    ResultSet result = stat.executeQuery("select * from EMPLOYEE");
    // 5、如果有查询结果:遍历结果
    while (result.next()) {
        int id = result.getInt(1);
        String name = result.getString(2);
        System.out.println(id + "--" + name);
    }
    JdbcUtils.release(result, stat, conn);
}
Csdn user default icon
上传中...
上传图片
插入图片