JDBC在使用过程中会发现获取连接以及释放资源的代码较为繁琐而且使用频率高,使用JDBC就会用到,通过封装成工具类的方式可以有效的简化代码,通过以下三种方式封装为工具类
1、直接封装
2、使用properties配置文件
2.1 JDK工具类ResourceBundle加载配置文件封装
2.2 类加载配置文件获取流,使用Properties对象
【1】直接封装
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCUtils_V1 {
// 1、获取连接的方法
public static Connection getConnection() {
try {
// 1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2、获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
return conn;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 2、释放资源的方法
// 多个try-catch块,将资源释放,容易理解
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
// try-catch-finally嵌套,资源释放时出错通知调用者
public static void release2(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e2) {
throw new RuntimeException(e2);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
}
【2.1】JDK工具类ResourceBundle加载配置文件封装
public class JDBCUtils_V2 {
private static String driver;
private static String url;
private static String user;
private static String password;
/*
* 配置文件只需要加载一次,所以放入静态代码块中,通过配置文件加载和ResouceBundle类
*/
static {
ResourceBundle bundle = ResourceBundle.getBundle("db");
driver = bundle.getString("jdbc.driver");
url = bundle.getString("jdbc.url");
user = bundle.getString("jdbc.user");
password = bundle.getString("jdbc.password");
}
public static Connection getConnection() {
Connection conn = null;
try {
// 1、注册驱动
Class.forName(driver);
// 2、获取连接
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 释放资源方法
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
【2.2】类加载配置文件获取流,使用Properties对象
package cn.itcast.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class JDBCUtils_V3 {
private static String driver;
private static String url;
private static String user;
private static String password;
static {
try {
// InputStream
// is=JDBCUtils_V3.class.getResourceAsStream("/db.properties");
// //不使用类加载器,获取src下的配置文件
// InputStream
// is=JDBCUtils_V3.class.getResourceAsStream("db2.properties");
// //不使用类加载器,获取当前类同包下的配置文件
// 获得类加载器的固定写法,当前类名.class.getClassLoader()
InputStream is = JDBCUtils_V3.class.getClassLoader().getResourceAsStream("db.properties"); // 使用类加载器
// 使用Properties对象处理流
// 使用load()方法加载指定 的流
Properties props = new Properties();
props.load(is);
// 使用getProperty(key),通过key获得需要的值
driver = props.getProperty("jdbc.driver");
url = props.getProperty("jdbc.url");
user = props.getProperty("jdbc.user");
password = props.getProperty("jdbc.password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
}
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
}
}
}
本文介绍了JDBC操作中如何通过封装工具类来简化获取数据库连接及释放资源的步骤,包括直接封装、使用Properties配置文件两种方式,详细讲解了ResourceBundle和利用类加载配置文件的方法。
1936

被折叠的 条评论
为什么被折叠?



