- 资源介绍
- 更新记录
package com.sd.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * * @author Administrator * 专门用来减少重复代码的 */ public class DBUtils { private static Properties properties = null; private static Connection connection = null; /** * 禁止实例化 */ private DBUtils(){} static { try { /** * 在类被加载的时候,读取配置文件中的值 * 将配置文件中的驱动加载 */ properties = FileUtil.getProperties(); Class.forName(properties.getProperty("driver")); } catch (Exception e) { e.printStackTrace(); } } /** * 创建数据库链接 * 懒加载 * @return * @throws SQLException */ public static Connection getConnection() throws SQLException{ /** * 没有的时候进行船舰 * 有的时候则进行直接使用 */ if(connection == null || connection.isClosed()){ connection = DriverManager.getConnection(properties.getProperty("url"),properties); } return connection; } /** * 用来关闭数据库信息的 * @param statement * @param connection * @param resultSet * @throws SQLException */ public static void close(Statement statement,Connection connection,ResultSet resultSet) throws SQLException{ if(statement != null ){ statement.close(); } if(connection != null ){ connection.close(); } if(resultSet != null ){ resultSet.close(); } } /** * 用来关闭数据库信息的 * @param statement * @param connection * @param resultSet * @throws SQLException */ public static void close(Statement statement,Connection connection) throws SQLException{ close(statement,connection,null); } }
因为使用了配置文件,所以需要进行加载
package com.sd.utils; import java.io.FileInputStream; import java.util.Properties; public class FileUtil { /** * 读取配置文件的方法 * @throws Exception */ public static Properties getProperties() throws Exception{ Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream("jdbc.properties"); properties.load(fileInputStream); return properties; } }
加载方式有了之后,当然需要配置文件咯,配置文件如下:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/student?characterEncoding=utf8 user=root password=123456
打完收工~
jar包在右侧下载栏~~~
猜你喜欢
-
通过代码bat在win桌面创建带图标的快捷方式
2022-09-26 -
全网最全的springboot 相关注解说明–持续更新
2022-05-16 -
java springboot 监听程序是否运行,并重启
2024-05-07 -
wp,ripro主题优化文章页内–分类设置标签云展示方式
2021-01-30 -
若依微服务式中 sentinel 限流理解
2022-08-24 -
springMVC接受list<对象>的值,通过form表单
2022-05-23 -
java小程序,拼图小程序,GUI小程序,SWING小程序
2020-12-06 -
Oracle查询数据库中所有表的记录数
2020-12-03 -
apache反向代理出现502调整
2020-12-03 -
linux shell脚本调用java main方法 代码传值
2020-12-03
-
2.若依使用说明,目录说明
2022-08-15 -
百度地图api 设置多点标注,点击标注后出现自定义内容
2022-11-27 -
java基础使用for循环 输出菲波拉契数列前30位
2021-09-22 -
http 工具类 httputils发送json请求
2023-02-21 -
Java 深入解析,强引用、弱引用、软引用、虚引用
2020-12-17 -
多线程-线程通讯之-管程法(生产者-消费者-缓存区)
2021-06-24 -
多线程-线程通讯之-管程法(生产者-消费者-缓存区)
2021-06-24 -
基于Java的连连看游戏设计与实现项目源码-GUI-SWING
2021-05-11 -
网页exe生成,通过代码创建网页快捷方式Bat To Exe Converter
2022-09-26 -
java 反射动态调用任意方法(有参数,无参数)
2021-12-03
猜你在找
1. 本站所有资源来源于用户上传和网络,如有侵权请及时联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"mababa.xin",如遇到无法解压的请联系管理员!
码巴巴-优质代码创造者 » jdbc 工具包实现,另附赠mysql-connector-java-5.1.46jar包
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 一个高级程序员模板开发平台
- 2022-03-18Hi,初次和大家见面了,请多关照!