DBCP2未连接数据库

时间:2019-05-24 07:27:38

标签: java oracle database-connection connection-string dbcp

我正在尝试使用首次连接池并编写如下代码

此代码正在编译但未运行

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;

public class DB1 {

    private static BasicDataSource ds = null;

    public DataSource setupDataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUsername("username");
        ds.setPassword("password");
        ds.setUrl("jdbc:oracle:thin:@165.175.28.15:1521/XE");
        return ds;
    }

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

我在这里尝试过article

import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author ashraf
 *
 */
public class DB {

    private static final String DRIVER_CLASS_NAME = "oracle.jdbc.driver.OracleDriver";
    private static final String DB_URL = "jdbc:oracle:thin:@163.173.29.15:1521/XE";
    private static final String DB_USER = "user";
    private static final String DB_PASSWORD = "pass";
    private static final int CONN_POOL_SIZE = 5;

    private BasicDataSource bds = new BasicDataSource();

    private DB() {
        //Set database driver name
        bds.setDriverClassName(DRIVER_CLASS_NAME);
        //Set database url
        bds.setUrl(DB_URL);
        //Set database user
        bds.setUsername(DB_USER);
        //Set database password
        bds.setPassword(DB_PASSWORD);
        //Set the connection pool size
        bds.setInitialSize(CONN_POOL_SIZE);
    }

    private static class DataSourceHolder {
        private static final DB INSTANCE = new DB();
    }

    public static DB getInstance() {
        return DataSourceHolder.INSTANCE;
    }

    public BasicDataSource getBds() {
        return bds;
    }

    public void setBds(BasicDataSource bds) {
        this.bds = bds;
    }

    public static Connection getOracleConnection() throws SQLException{
        BasicDataSource bds = DB.getInstance().getBds();
        Connection cnn = bds.getConnection();
    return cnn;
    }
}

这会引发错误:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message Servlet execution threw an exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.NoClassDefFoundError: DB$DataSourceHolder
    DB.getInstance(DB.java:37)
    DB.getOracleConnection(DB.java:49)
    IndexDAO.displayNewsTicker(IndexDAO.java:55)
    SiteTemplate.newsTicker(SiteTemplate.java:256)
    SiteTemplate.headerButtons(SiteTemplate.java:226)
    Index.doGet(Index.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

java.lang.ClassNotFoundException: DB$DataSourceHolder
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    DB.getInstance(DB.java:37)
    DB.getOracleConnection(DB.java:49)
    IndexDAO.displayNewsTicker(IndexDAO.java:55)
    SiteTemplate.newsTicker(SiteTemplate.java:256)
    SiteTemplate.headerButtons(SiteTemplate.java:226)
    Index.doGet(Index.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.

这将是我的第一个连接池课程,但是仍然没有运气来解决它。请告知我在哪里犯错?有任何建议吗

0 个答案:

没有答案