A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 ying 于 2013-1-8 13:15 编辑

package utile;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBUtileC3P0 {

        private static ComboPooledDataSource cpd = null;
        private static DBUtileC3P0 instance = null;
        String className = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String username = "system";
        String password = "system";
        private DBUtileC3P0()
        {
                //设置jdbc信息
                cpd.setUser(username);
                cpd.setPassword(password);
                cpd.setJdbcUrl(url);
                try {
                        cpd.setDriverClass(className);
                } catch (PropertyVetoException e) {
                        e.printStackTrace();
                }
                //连接池的设置
                cpd.setInitialPoolSize(30);
                cpd.setMaxPoolSize(1000);
                cpd.setMinPoolSize(10);
               
        }
        
        public static DBUtileC3P0 getInstance()
        {
                if(instance == null)
                {
                        instance = new DBUtileC3P0();
                }
                return instance;
        }
        public Connection getConnection() throws SQLException
        {

                return cpd.getConnection();
        }
        public static void main(String[] args) throws SQLException {
                Connection conn = DBUtileC3P0.getInstance().getConnection();

                if( null != conn)
                {
                        System.out.println("数据库连接失败");
                }
               
        }
}

未命名.jpg (51.13 KB, 下载次数: 25)

数据库连接失败的输出

数据库连接失败的输出

2 个回复

倒序浏览
空指针啊,调试一下看看那个地方是空指针啊
回复 使用道具 举报
郝福明 发表于 2013-1-1 09:44
空指针啊,调试一下看看那个地方是空指针啊

嗯!谢啦!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马