当前位置:网站首页>JDBC工具类
JDBC工具类
2022-07-23 01:17:00 【汤键.】
目录
- 之所以使用工具类,是为了避免代码冗余
编写配置文件
- 在src目录下创建config.properties配置文件

driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/dp1 username=root password=123456工具类的编写
- 过程
- 1.私有构造方法
- 目的是为了不让其它类去创建对象
- 因为既然作为一个工具类来使用的话
- 那么这个类当中所有方法都是静态的
- 2.声明所需要的配置变量
- 3.提供静态代码块,读取配置文件信息为变量赋值,注册驱动
- 4.提供获取数据库连接方法
- 5.提供释放资源的方法
- 实例



package demo02.utils; import java.io.InputStream; import java.sql.*; import java.util.Properties; //JDBC工具类 public class JDBCUtils { //1.私有构造方法 private JDBCUtils(){} //2.声明所需要的配置变量 private static String driverClass; private static String url; private static String username; private static String password; private static Connection con; //3.提供静态代码块;读取配置文件信息为变量赋值,注册驱动 static{ try { //读取配置文件的信息为变量赋值 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties"); Properties prop = new Properties(); prop.load(is); driverClass = prop.getProperty("driverClass"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); //注册驱动 Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); } } //4.提供获取数据库连接的方法 public static Connection getConnection() { try { con = DriverManager.getConnection(url,username,password); } catch (SQLException e) { e.printStackTrace(); } return con; } //5.释放资源的方法 public static void close(Connection con, Statement stat, ResultSet rs) { if(con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(Connection con, Statement stat) { if(con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } } }优化学生案例
- 1.将之前的注册驱动和获取数据库连接删除
- 直接通过工具类调用静态方法就能拿到数据库并完成注册驱动

- 2.然后在释放资源这也将之前的删掉
- 直接通过工具类调用close方法就完成了

//以删除学生信息为例 public int delete(Integer id) { Connection con = null; Statement stat = null; int result = 0; try { con = JDBCUtils.getConnection(); //3.获取执行者对象 stat = con.createStatement(); //4.执行sql语句,并且接收返回的结果集 String sql = "DELETE FROM student WHERE sid='" + id + "'"; result = stat.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { //6.释放资源 JDBCUtils.close(con, stat); //将结果返回 return result; } }
边栏推荐
- Understand the box model, and the basic methods of box model's frame, internal and external margins, horizontal layout, vertical layout, setting floating, and dealing with height collapse
- BCG 使用之CBCGPColorDialog控件
- What is the combined effect of compose and recyclerview?
- Number theory -- division and blocking, common classic examples.
- Is it safe to buy shares and open an account? Will you lose money?
- 【FPGA教程案例36】通信案例6——基于vivado核的FFT傅里叶变换开发以及verilog输入时序配置详解,通过matlab进行辅助验证
- 涨薪神器
- PyG利用MessagePassing搭建GCN实现节点分类
- [Huawei online battle service] how can new players make up frames when the client quits reconnection or enters the game halfway?
- SPSS Chi-Square
猜你喜欢

SPSS Chi-Square

C语言实战之猜数游戏

Const char* in vs2022 cannot assign char*

Event listening and deleting events - event object - default event - cancel bubbling event - event delegation - default trigger

数学建模——图与网络模型及方法(二)
![[wechat applet] Introduction to development (2)](/img/8c/92526faee083cd12562fd6adf7efc4.png)
[wechat applet] Introduction to development (2)

砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行

【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?
How many points can you get on the latest UnionPay written test for test engineers?

【微信小程序】开发入门篇(二)
随机推荐
[ManageEngine] six essential functions of network configuration management
【FPGA教程案例36】通信案例6——基于vivado核的FFT傅里叶变换开发以及verilog输入时序配置详解,通过matlab进行辅助验证
The role of include in makefile
[LeetCode]剑指 Offer 61. 扑克牌中的顺子
开发者必看 | DevWeekly 第1期:什么是时间复杂度?
La fosse Piétinée par l'homme vous dit d'éviter les 10 erreurs courantes dans les tests automatisés
Talk about HART Protocol
-Bash: wget: command not found
为什么使用Well-Architected Framework?
【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙
C language classic exercise (1) - "daffodil number"“
在线抠图和换背景及擦除工具
PMP一手资料、一手资讯获取
在通达信开户安全不
Avantages de la salle des machines bgp
Is it safe to buy shares and open an account? Will you lose money?
Stream操作之 先分组再取最大值
BGP機房的優點
C#之winform窗体的最大化、最小化、还原、关闭以及窗体的移动
2022.7.22-----leetcode.757





