Spring整合JDBC(dbcp,C3P0,Druid,JdbcTemplate )

一、整合数据库连接池dbcp

1.导入jar包

<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.31</version>
    </dependency>

2.添加配置文件spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

 <!--开启注解-->
    <context:annotation-config/>
    <!--开启组件扫描-->
    <context:component-scan base-package="com.yy"/>

</beans>

3.配置数据源

<--DBCP连接池配置-->
    <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ball?serverTimezone=GMT%2B8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123sa"></property>
        </bean>

4.使用数据库连接池实现原生的jdbc添加用户

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private BasicDataSource dataSource;
    @Override
    public void aa(User user) {
        Connection connection=null;
        try {
            connection = dataSource.getConnection();
            String sql="insert into user values(null,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPwd());
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

二、整合数据库连接池C3P0

1.导入jar包

 <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.31</version>
    </dependency>

2.添加配置文件spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

 <!--开启注解-->
    <context:annotation-config/>
    <!--开启组件扫描-->
    <context:component-scan base-package="com.yy"/>

</beans>

3.配置数据源

<!--C3P0连接池配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ball?serverTimezone=GMT%2B8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123sa"></property>
    </bean>

4.使用数据库连接池实现原生的jdbc添加用户

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private ComboPooledDataSource dataSource;
    @Override
    public void aa(User user) {
        Connection connection=null;
        try {
            connection = dataSource.getConnection();
            String sql="insert into user values(null,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPwd());
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

三、整合数据库连接池Druid

1.导入jar包

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.15</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.31</version>
    </dependency>

2.添加配置文件spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

 <!--开启注解-->
    <context:annotation-config/>
    <!--开启组件扫描-->
    <context:component-scan base-package="com.yy"/>

</beans>

3.配置数据源

<!--Druid连接池配置-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ball?serverTimezone=GMT%2B8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123sa"></property>
    </bean>

4.使用数据库连接池实现原生的jdbc添加用户

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private DruidDataSource dataSource;
    @Override
    public void aa(User user) {
        Connection connection=null;
        try {
            connection = dataSource.getConnection();
            String sql="insert into user values(null,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPwd());
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

四、整合spring 内置数据源(JdbcTemplate

1.导入jar包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.24</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.31</version>
    </dependency>

2.添加配置文件spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

 <!--开启注解-->
    <context:annotation-config/>
    <!--开启组件扫描-->
    <context:component-scan base-package="com.yy"/>

</beans>

3.配置数据源

 <!--JdbcTemplate连接池配置-->
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ball?serverTimezone=GMT%2B8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123sa"></property>
    </bean>

4.使用内置数据源实现原生的jdbc添加用户

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private DriverManagerDataSource dataSource;
    @Override
    public void aa(User user) {
        Connection connection=null;
        try {
            connection = dataSource.getConnection();
            String sql="insert into user values(null,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getName());
            ps.setString(2, user.getPwd());
            ps.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }finally {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值