DruidUtils 和 JdbcTemplate配置和使用

在这里插入图片描述

方式一:


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class MyTest {  
    @Test
    public void jdbcTemplateTest(){
        //使用JdbcTemplate完成数据库表建立
        //1.创建数据库连接池,使用spring内置的连接池
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///flysun");
        dataSource.setUsername("root");
        dataSource.setPassword("xxxxx");
        
        //2.通过连接池构造模板对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        
        //3.使用Template 执行sql语句
        jdbcTemplate.execute("create table person (id int primary key, name varchar(20))"); 
        jdbcTemplate.query("select * from table where id = ?",id);            
    }
 
}

方式二,使用properties配置

package Utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DruidUtlis {
    private static DataSource dataSource=null;
    static{
        InputStream resourceAsStream = DruidUtlis.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    public static DataSource getDataSource() throws SQLException {
        return dataSource;    }
}

db.properties 文件

	url=jdbc:mysql://localhost:3306/test
	username=root
	password=root
	driverClassName=com.mysql.jdbc.Driver
	# \u8FDE\u63A5\u6C60\u7684\u53C2\u6570
	initialSize=10
	#\u8FDE\u63A5\u6C60\u4E2D\u6700\u5927\u652F\u630110\u4E2A\u8FDE\u63A5
	maxActive=10
	#z\u6700\u5927\u7B49\u5F85\u65F6\u95F4 2\u79D2
	maxWait=5000

使用:

//2.通过连接池构造模板对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtlis.getDataSource());
        
        //3.使用Template 执行sql语句
        jdbcTemplate.execute("create table person (id int primary key, name varchar(20))"); 
        jdbcTemplate.query("select * from table where id = ?",id);      
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值