目的:简化书写
分析:
    驱动注册,连接对象创建,其中包括输入驱动,数据库的地址,以及用户名和密码,每次编写代码都需要重复编写,如果每次使用的都是同一个账户的同一个数据库,代码的重复读很高,甚至如果sql语句相同,或者都是查询语句时,会创建的statement对象,而对象的销毁程序也是一模一样。面对这样高度重复的代码,我们需要对其进行封装。像数据库信息这种可能会变更的信息,我们可以写一个数据库的配置文件,当用户的数据库信息不变时,直接使用其中的信息即可,如果有变更,直接更改配置文件,再进行数据读写。
 
实现:
package demo02.homework;

import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.*;
import java.util.Properties; public class JdbcUtils {
private static String url = null;
private static String user = null;
private static String password = null;
private static String driver = null; // 使用静态方法,保持全局调用
static {
// 获取包所在文件夹的数据库配置文件
ClassLoader classLoader = JdbcUtils.class.getClassLoader();
// getResource内的数据库配置文件路径最好写绝对路径
URL resource = classLoader.getResource("demo02/homework/jdbc.properties");
String path = resource.getPath();
try {
// 如果数据库配置文件路径有中文,会显示乱码,需要进行解码
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 创建获取配置信息对象
Properties prop = new Properties();
try {
// 加载数据库配置文件
prop.load(new FileReader(path));
} catch (IOException e) {
e.printStackTrace();
}
// 获取数据库配置文件对应的信息
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
driver = prop.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} // 创建连接对象
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
} // 关闭对象方法,这里进行了重载,接收不同的对象
public static void close(Connection conn, Statement stat){
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(Connection conn, Statement stat, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

最新文章

  1. 基于JQuery的浮动DIV显示提示信息并自动隐藏
  2. csuoj 1391: Boiling Vegetables
  3. 【实践】js实现简易的四则运算计算器
  4. struts2+spring+hibernte整合示例
  5. 人人都是 DBA(IV)SQL Server 内存管理
  6. PHP undefined index的解决办法
  7. DllImport属性详解
  8. 小议jQuery中的事件
  9. (JAVA版)冒泡排序
  10. Swift中文教程(六)--枚举和结构
  11. 读书笔记 effective c++ Item 36 永远不要重新定义继承而来的非虚函数
  12. 笔记:MyBatis XML配置-typeHandlers 默认类型处理器
  13. 配置mongo
  14. shell中脚本变量和函数变量的作用域
  15. [UE4]认识Decorator
  16. 银行手机APP安全评估报告【转载】
  17. 04_web基础(六)之请求转发与重定向
  18. 从错误中学python(4)——最小公约数与辗转相除法
  19. C++ 为什么要使用#ifdef __cplusplus extern "C" { #endif
  20. Binwalk:后门(固件)分析利器

热门文章

  1. Maven配置 阿里云镜像地址
  2. 下午小博(java小知识)
  3. Error: EPERM: operation not permitted, mkdir ‘C:\Program Files\nodejs‘TypeError: Cannot read proper
  4. .net core 前端传递参数有值 后端接收到的数据却是null
  5. Consul+SpringCloud微服务(入门三)
  6. ajax的原理是什么?如何实现?
  7. 自动化测试如此容易!多语言自动化测试框架 Selenium 编程(C#篇)
  8. day07-MyBatis的关联映射01
  9. [转载]pytest报AttributeError: module ‘pytest‘ has no attribute ‘main‘
  10. 问题集锦 ~ javascript