引入所需要的jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0 .11 </version>
</dependency>
创建属性文件
druid_system_dev.properties
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql:
username = test
password = WI9nfppbYoWXxB6iVcdXWk3BJM+qfOkBNgDdEZ3yeYEyPWniw8hLcDG6k
#这个密码是通过加密后的密码,加密方法:找到 druid-0.2 .9 .jar所在目录,
#执行 java -cp druid-0.2 .9 .jar com.alibaba.druid.filter.config.ConfigTools you_password #加密
initialSize = 5
maxActive = 10
minIdle = 3
maxWait = 60000
removeAbandoned = true
removeAbandonedTimeout = 180
timeBetweenEvictionRunsMillis = 60000
minEvictableIdleTimeMillis = 300000
validationQuery = SELECT 1 FROM DUAL
testWhileIdle = true
testOnBorrow = false
testOnReturn = false
poolPreparedStatements = true
maxPoolPreparedStatementPerConnectionSize = 50
filters = config
connectionProperties = config.decrypt=true
java代码
InputStream in = Database.class.getClassLoader().getResourceAsStream();
Properties props = new Properties("druid_system_dev.properties" );
props.load(in);
DataSource ds = DruidDataSourceFactory.createDataSource(props);
Connection connection = ds.getConnection();
String sql = "select * from a" ;
PreparedStatement prepstmt = connection.prepareStatement(sql);
ResultSet rs = prepstmt.executeQuery(sql);