Druid工具类
定义Druid工具类的步骤:
- 定义一个Druid类
- 提供静态代码块加载配置文件,初始化连接池对象
- 提供方法
- 获取连接方法(getConnection):通过数据库连接池获取连接
- 释放资源
- 获取连接池的方法(DataSource)
package com.haikang.druid;
/**
* Druid工具类
* 1. 定义一个Druid类
* 2. 提供静态代码块加载配置文件,初始化连接池对象
* 3. 提供方法
* 1. 获取连接方法(getConnection):通过数据库连接池获取连接
* 2. 释放资源
* 3. 获取连接池的方法(DataSource)
*/
//1. 定义一个Druid类
public class DruidUtils {
//定义DataSource变量
private static DataSource dataSource = null;
//2. 提供静态代码块加载配置文件,初始化连接池对象
static {
try {
//加载配置文件druid.properties
Properties properties = new Properties();
properties.load(DebugUtils.class.getClassLoader().getResourceAsStream("druid.properties"));//加载文件
//获得数据库连接池对象
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 1. 获取连接方法(getConnection):通过数据库连接池获取连接
*/
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
/**
* 2. 释放资源
*/
public static void close(Statement statement,Connection connection){
// if (statement!=null){
// try {
// statement.close();
// } catch (SQLException throwables) {
// throwables.printStackTrace();
// }
// }
// if (connection!=null){
// try {
// statement.close();
// } catch (SQLException throwables) {
// throwables.printStackTrace();
// }
// }
close(null,statement,connection);
}
public static void close(ResultSet resultSet,Statement statement,Connection connection){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
* 获取数据库连接池对象DataSource
*/
public static DataSource getDataSource(){
return dataSource;
}
}
package com.haikang;
/**
* DruidUtils工具类的测试
* 向account表插入一条记录
*/
public class DruidUtilsTest {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//获取数据库连接对象
connection = DruidUtils.getConnection();
//定义sql语句
String sql = "insert into account values (null,?,?)";
//获取Sql执行对象
preparedStatement = connection.prepareStatement(sql);
//给占位符赋值
preparedStatement.setString(1,"南宁");
preparedStatement.setDouble(2,4988);
//执行Sql
int i = preparedStatement.executeUpdate();
//处理返回结果
System.out.println(i);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//关流
finally {
DruidUtils.close(preparedStatement,connection);
}
}
}
package com.haikang;
/**
* DruidUtils工具类的测试
* 查询account表的所有记录
*/
public class DruidUtilsTest2 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//获得数据库连接池
connection = DruidUtils.getConnection();
//定义Sql语句
String sql = "select * from account where id=?";
//获得Sql执行语句
preparedStatement = connection.prepareStatement(sql);
//给占位符赋值
preparedStatement.setInt(1,1);
//执行Sql语句
resultSet = preparedStatement.executeQuery();
//处理结果
while (resultSet.next()!=false){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
double aDouble = resultSet.getDouble(3);
System.out.println(id+"\t"+name+"\t"+aDouble);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
DruidUtils.close(resultSet,preparedStatement,connection);
}
}
}