赞
踩
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
DruidDataSource大部分属性都是参考DBCP的,如果你原来就是使用DBCP,迁移是十分方便的。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_user}" /> <property name="password" value="${jdbc_password}" /> <property name="filters" value="stat" /> <property name="maxActive" value="20" /> <property name="initialSize" value="1" /> <property name="maxWait" value="6000" /> <property name="minIdle" value="1" /> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="poolPreparedStatements" value="true" /> <property name="maxOpenPreparedStatements" value="20" /> <property name="asyncInit" value="true" /> </bean>
这个是 spring 的配置,其实配置上就是一个 POJO
use test;
CREATE TABLE "users" (
"id" int(11) NOT NULL,
"username" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
insert into users (id, username, email) values (1, 'u-1', '1@email.com');
insert into users (id, username, email) values (2, 'u-2', '2@email.com');
insert into users (id, username, email) values (3, 'u-3', '3@email.com');
mysql> select * from users;
+----+----------+-------------+
| id | username | email |
+----+----------+-------------+
| 1 | u-1 | 1@email.com |
| 2 | u-2 | 2@email.com |
| 3 | u-3 | 3@email.com |
+----+----------+-------------+
3 rows in set (0.00 sec)
<!-- MySQL JDBC Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version> <!-- 或者最新版本 -->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.15</version>
</dependency>
package com.github.houbb.calcite.learn.mysql; import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * druid 整合 mysql 使用 * @author 老马啸西风 */ public class DruidMySQLExample { public static void main(String[] args) { // 初始化 Druid 数据源 DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"); dataSource.setUsername("admin"); dataSource.setPassword("123456"); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 从连接池获取数据库连接 conn = dataSource.getConnection(); // 创建 Statement 对象 stmt = conn.createStatement(); // 执行 SQL 查询 rs = stmt.executeQuery("SELECT * FROM users"); // 遍历结果集 while (rs.next()) { // 处理每一行数据 int id = rs.getInt("id"); String username = rs.getString("username"); String email = rs.getString("email"); // 输出到控制台 System.out.println("ID: " + id + ", username: " + username+ ", email: " + email); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
输出如下:
ID: 1, username: u-1, email: 1@email.com
ID: 2, username: u-2, email: 2@email.com
ID: 3, username: u-3, email: 3@email.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。