- 资源介绍
- 更新记录
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包在右侧下载栏~~~
猜你喜欢
-
springMVC接受list<对象>的值,通过form表单
2022-05-23 -
tomcat 搭建图片访问服务,解决百度ueditor 富文本回显问题
2022-10-28 -
Calendar 万年历java,GUIM,SWING
2020-12-06 -
vue 创建公共js 方式
2022-11-04 -
java实现多线程的三种方式之Thread
2021-06-24 -
IO流-缓冲流–BufferedReader
2021-06-26 -
多线程-线程通讯之-管程法(生产者-消费者-缓存区)
2021-06-24 -
若依微服务版通过若依框架包名修改器下载的项目需要修改的地方
2022-12-01 -
java基础使用for循环 输出菲波拉契数列前30位
2021-09-22 -
Oracle查询数据库中所有表的记录数
2020-12-03
-
wp,ripro主题优化文章页内–分类设置标签云展示方式
2021-01-30 -
maven官网,maven依赖下载位置,Maven仓库官网网址
2023-01-28 -
百度地图api 设置多点标注,点击标注后出现自定义内容
2022-11-27 -
若依微服务式中 sentinel 限流理解
2022-08-24 -
java通过http服务执行shell命令
2021-01-19 -
fhbootvfm-oa,fhadmin项目中,百度ueditor 富文本使用
2022-10-26 -
多线程-线程通讯之-管程法(生产者-消费者-缓存区)
2021-06-24 -
全网最全的springboot 相关注解说明–持续更新
2022-05-16 -
基于Java的连连看游戏设计与实现项目源码-GUI-SWING
2021-05-11 -
IO流之-转换流-InputStreamReader
2021-06-25
猜你在找
1. 本站所有资源来源于用户上传和网络,如有侵权请及时联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"mababa.xin",如遇到无法解压的请联系管理员!
码巴巴-优质代码创造者 » jdbc 工具包实现,另附赠mysql-connector-java-5.1.46jar包
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 一个高级程序员模板开发平台
- 2022-03-18Hi,初次和大家见面了,请多关照!