Exception java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource

后端 未结 6 1881
广开言路
广开言路 2021-02-07 07:07

I\'m trying to integrate spring and Hibernate with mysql. I created a simple java project and a package with 3 classes, an application context.xml file, and an hbm.xml for mappi

相关标签:
6条回答
  • 2021-02-07 07:10

    It looks like that jvm could not find the org.apache.commons.dbcp.BasicDataSource class.Please check in your class-path or in your project build-path that commons-dbcp.jar is present or not.

    If yes then, open your commons-dbcp.jar and check that particular class(org.apache.commons.dbcp.BasicDataSource) is available or not

    0 讨论(0)
  • 2021-02-07 07:19

    change org.apache.commons.dbcp.BasicDataSource to org.apache.commons.dbcp2.BasicDataSource,it works well

    0 讨论(0)
  • 2021-02-07 07:25

    I got the same Exception stack with DBCP2. The issue was that my local m2 cache of the JAR file was corrupt. After I deleted it, Maven said it could not download 2.1.1. I switched to 2.5.0 and the app loaded successfully.

    0 讨论(0)
  • 2021-02-07 07:26

    springframework.jdbc.datasource.DriverManagerDataSource instead of org.apache.commons.dbcp.BasicDataSource in applicationContext.xml file. It works fine.

    0 讨论(0)
  • 2021-02-07 07:33

    Your problem is Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource on "dataSource" bean.

    If using maven add this dependency

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.2.2</version>
    </dependency>
    

    else add this library commons-dbcp-jar in your classpath.

    0 讨论(0)
  • 2021-02-07 07:34

    If you are using Spring 4+, you should use SessionFactory. Also, you need these jars: commons-pool, commons-dbcp.

    This is a sample code that I made, I hope it helps you:

    public class EmployeeDAOImpl implements EmployeeDAO{
    
        private SessionFactory sessionFactory;
    
        @Override
        public void saveEmployee(Employee e) {
            Session session = this.sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            session.persist(e);
            tx.commit();
            session.close();
        }
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
    }
    

    applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin@:..." />
            <property name="username" value="admin" />
            <property name="password" value="admin" />
        </bean>
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
            <property name="dataSource" ref="dataSource"></property>
    
            <property name="mappingResources">
            <list>
                <value>employee.hbm.xml</value>
            </list>
            </property>
    
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
    
                </props>
            </property>
        </bean>
    
    
        <bean id="d" class="com.orm.EmployeeDAOImpl">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    
    </beans>
    
    0 讨论(0)
提交回复
热议问题