Druid简单使用
一、添加maven依赖
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency>
二、添加连接池配置(durid.properties)
driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://10.102.16.64\:3306/wind_wfcmisc_db?useUnicode\=true&characterEncoding\=utf-8 username=sa password=abcd1234 filters=stat initialSize=2 maxActive=300 maxWait=60000 timeBetweenEvictionRunsMillis=60000 minEvictableIdleTimeMillis=300000 validationQuery=SELECT 1 testWhileIdle=true testOnBorrow=false testOnReturn=false poolPreparedStatements=false maxPoolPreparedStatementPerConnectionSize=200
三、简单使用
public class DruidTest { public static void main(String[] args) throws Exception { // 读取连接池配置 Properties properties = new Properties(); properties.load(new FileInputStream("D:\\eclipse_workspace\\maven-test4\\src\\main\\resources\\durid.properties")); // 获取数据源 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); // 获取连接 Connection conn = dataSource.getConnection(); // 执行sql语句 PreparedStatement ps = conn.prepareStatement("select * from tb_customermanage_risk_type"); ResultSet rs = ps.executeQuery(); while(rs.next()) { System.out.println(rs.getString(2)); } } }