传统的jdbc连接数据库方式如下:
我们需要几个步骤:注册 JDBC 驱动程序注册( Class.forName(DRIVER_NAME) ),通过DriverManager获取物理连接。
一次数据库访问对应一个物理连接,每次操作数据库都要打开、关闭该物理连接。
添加mysql驱动包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
jdbc操作类如下:
public class DBUtil {
private static Logger logger = LoggerFactory.getLogger(DBUtil.class);
private static String DRIVER_NAME = null;
private static String URL = null;
private static String USER_NAME = null;
private static String PWD = null;
private static Properties properties = new Properties();
static {
try {
properties.load(DBUtil.class.getResourceAsStream("/jdbc.properties"));
} catch (IOException e) {
logger.error("系统加载jdbc.properties配置异常");
}
DRIVER_NAME = properties.getProperty("jdbc.driver");
URL = properties.getProperty("jdbc.url");
USER_NAME = properties.getProperty("jdbc.username");
PWD = properties.getProperty("jdbc.password");
try {
Class.forName(DRIVER_NAME);
} catch (ClassNotFoundException e) {
logger.error("加载数据库驱动异常");
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USER_NAME, PWD);
} catch (SQLException e) {
logger.error("创建数据库链接异常", e);
}
return connection;
}
public static void close(Connection connection) {
if (connection != null) {
try {
if (!connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
logger.error("连接关闭异常", e);
}
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
if (!resultSet.isClosed()) {
resultSet.close();
}
} catch (SQLException e) {
logger.error("结果集关闭异常", e);
}
}
if (statement != null) {
try {
if (!statement.isClosed()) {
statement.close();
}
} catch (SQLException e) {
logger.error("语句关闭异常", e);
}
}
close(connection);
}
}
我们采用DBCP(DataBase connection pool),数据库连接池。DBCP是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。
添加dbcp依赖,对应commons-pool和commons-dbcp两个包
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
使用连接池,代码如下:
public class DBCPUtil {
private static Logger logger = LoggerFactory.getLogger(DBUtil.class);
private static Properties properties = new Properties();
private static DataSource dataSource;
static {
try {
properties.load(DBCPUtil.class.getResourceAsStream("/jdbc.properties"));
} catch (IOException e) {
logger.error("系统加载jdbc.properties配置异常");
}
try {
dataSource= BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
logger.error("加载数据库驱动异常");
}
// dataSource=new BasicDataSource();
// dataSource.setUsername("root");
// dataSource.setPassword("root");
// dataSource.setUrl("jdbc:mysql://localhost:3306/web");
// dataSource.setDriverClassName("com.mysql.jdbc.Driver");
}
public static Connection getConnection() {
Connection connection = null;
try {
connection=dataSource.getConnection();
connection.setAutoCommit(true);
} catch (SQLException e) {
logger.error("创建数据库链接异常", e);
}
return connection;
}
public static void close(Connection connection) {
if (connection != null) {
try {
if (!connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
logger.error("连接关闭异常", e);
}
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
if (!resultSet.isClosed()) {
resultSet.close();
}
} catch (SQLException e) {
logger.error("结果集关闭异常", e);
}
}
if (statement != null) {
try {
if (!statement.isClosed()) {
statement.close();
}
} catch (SQLException e) {
logger.error("语句关闭异常", e);
}
}
close(connection);
}
}
注意properties的配置 和jdbc是不同的。 我因为还用原来的配置,报错空指针,Cannot create JDBC driver of class '' for connect URL 'null' ,找了半天Bug
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/web?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
#jdbc.username=root
#jdbc.password=root
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/web?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=root
JDBC的数据库连接池使用javax.sql.DataSource来表示,DataSource只是一个接口,该接口通常由商用服务器等提供实现,也有一些开源组织提供实现(DBCP、C3P0)。
DataSource通常被称为数据源,它包含连接池和连接池管理两个部分,但习惯上也经常把DataSource称为连接池。
数据源连接池的方式连接数据库是在程序中,通过向一个JNDI(Java Naming and Directory Interface)服务器查询,即调用Context接口的lookup()方法,来得到DataSource对象,然后调用DataSource对象的getConnection()方法建立连接
在代码中使用DriverManager获得数据库连接的方式中,客户程序得到的连接对象是物理连接,调用连接对象的close()方法将关闭连接,而采用连接池技术,客户程序得到的连接对象是连接池中物理连接的一个句柄,调用连接对象的close()方法,物理连接并没有关闭,数据源的实现只是删除了客户程序中的连接对象和池中的连接对象之间的联系.
关于jndi,可以参考:http://benweizhu.github.io/blog/2014/07/07/learning-jdbc-with-jndi/