目录
二、使用 properties 配置文件封装 JDBCUtils 工具类
一、直接封装 JDBCUtils 工具类
在 JDBC 开发中,发现有很多代码都是重复操作,如“获得数据库连接”、“关闭资源”等,因此,可以封装自己的工具类 JDBCUtils,提供获取对象连接的方法,从而达到代码重复利用
封装自己的工具类
public class JDBCUtils {
private JDBCUtils() {
}
private static Connection con;
//定义静态代码块,最先执行
static {
try {
//1.注册驱动 反射技术,将驱动类加入到内容
Class.forName("com.mysql.jdbc.Driver");
//2.获得数据库连接 DriverManager类中静态方法
String url = "jdbc:mysql://localhost:3306/mydb";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
}catch (Exception ex){
throw new RuntimeException(ex + "连接数据库失败!");
}
}
//3.定义静态方法,返回数据库的连接对象
public static Connection getConnertion(){
return con;
}
//4.释放资源,由于释放资源的时候会有不同,可以采用方法重载
public static void close(Connection con, PreparedStatement pre){
if(con != null){
try{
con.close();
}catch(SQLException ex){}
}
if(pre != null){
try{
pre.close();
}catch(SQLException ex){}
}
}
//释放资源(重载)
public static void close(Connection con, PreparedStatement pre, ResultSet res){
if(con != null){
try{
con.close();
}catch(SQLException ex){}
}
if(pre != null){
try{
pre.close();
}catch(SQLException ex){}
}
if(res != null){
try{
res.close();
}catch(SQLException ex){}
}
}
}
在main中调用
public static void main(String[] args) throws SQLException {
//1.获得数据库连接
Connection con = JDBCUtils.getConnertion();
String sql = "select * from student";
//2.获得预处理对象
PreparedStatement pre = con.prepareStatement(sql);
//3.执行SQL语句
ResultSet res = pre.executeQuery();
while (res.next()){
System.out.println(res.getString("sno") + " " + res.getString("sname") + " " +
res.getString("ssex") + " " + res.getString("sage"));
}
//4.释放资源
JDBCUtils.close(con,pre,res);
}
二、使用 properties 配置文件封装 JDBCUtils 工具类
1、使用 Properties 配置文件
在开发中,会获得连接的四个参数:驱动、URL、用户名、密码,通常存放在配置文件中,方便后期的维护,程序如果需要更换数据库,只需要更换配置文件即可,通常,对配置文件做如下要求:
- 文件位置:建议放在 src 下
- 文件名称:扩展名为 properties
- 文件内容:一行一组数据,格式为: “key=walue”
(1) key:命名自定义,如果是多个单词,习惯使用点分隔,如:jdbc.driver
(2) value:值不支持中文,如果需要使用非英文字符,将进行 Unicode 转换
2、创建配置文件
在 src 目录下创建 database.properties 文件,文件内容如下:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=123456
3、加载配置文件
- 加载database.properties配置文件
- 使用IO读取文件,将键值对存储到集合中
- 从集合中以键值对的方式获取数据库的连接信息完成数据库的连接
封装自己的工具类并加载配置文件
/**
* 加载database.properties配置文件
* 使用IO读取文件,将键值对存储到集合中
* 从集合中以键值对的方式获取数据库的连接信息完成数据库的连接
*/
public class JDBCProUtlis {
private JDBCProUtlis() {
}
private static Connection con;
//定义静态代码块,最先执行
static {
//加载database.properties配置文件
InputStream in = Main.class.getClassLoader().getResourceAsStream("database.properties");
System.out.println(in);
Properties pro = new Properties();
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//使用IO读取文件,将键值对存储到集合中
String driverClass = pro.getProperty("driverClass");
String url = pro.getProperty("url");
String username = pro.getProperty("username");
String password = pro.getProperty("password");
try {
//1.注册驱动 反射技术,将驱动类加入到内容
Class.forName(driverClass);
//2.获得数据库连接 DriverManager类中静态方法
con = DriverManager.getConnection(url, username, password);
}catch (Exception ex){
throw new RuntimeException(ex + "连接数据库失败!");
}
}
//3.定义静态方法,返回数据库的连接对象
public static Connection getConnertion(){
return con;
}
//4.释放资源,由于释放资源的时候会有不同,可以采用方法重载
public static void close(Connection con, PreparedStatement pre){
if(con != null){
try{
con.close();
}catch(SQLException ex){}
}
if(pre != null){
try{
pre.close();
}catch(SQLException ex){}
}
}
//释放资源(重载)
public static void close(Connection con, PreparedStatement pre, ResultSet res){
if(con != null){
try{
con.close();
}catch(SQLException ex){}
}
if(pre != null){
try{
pre.close();
}catch(SQLException ex){}
}
if(res != null){
try{
res.close();
}catch(SQLException ex){}
}
}
}
在main中调用
public static void main(String[] args) throws SQLException {
//1.获得数据库连接
Connection con = JDBCProUtlis.getConnertion();
String sql = "select * from student";
//2.获得预处理对象
PreparedStatement pre = con.prepareStatement(sql);
//3.执行SQL语句
ResultSet res = pre.executeQuery();
while (res.next()){
System.out.println(res.getString("sno") + " " + res.getString("sname") + " " +
res.getString("ssex") + " " + res.getString("sage"));
}
//4.释放资源
JDBCProUtlis.close(con,pre,res);
}