JAVA连接、操作数据库的DBHelper
工厂模式的DBHelper
1 import java.sql.Connection;
2 import java.sql.DriverManager;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.Statement;
6
7 /**
8 * 数据库工具类,负责完成打开、关闭数据库,执行查询或更新
9 * @author MKing
10 *
11 */
12 public class DbHelper {
13 /**
14 * 数据库URL
15 */
16 private static final String URL = "jdbc:mysql://localhost:3306/bookstore";
17 /**
18 * 登录用户名
19 */
20 private static final String USER = "root";
21 /**
22 * 登录密码
23 */
24 private static final String PASSWORD = "12345";
25
26 private static Connection connection = null;
27 private static Statement statement = null;
28
29 private static DbHelper helper = null;
30
31 static {
32 try {
33 Class.forName("com.mysql.jdbc.Driver");
34 } catch (ClassNotFoundException e) {
35 e.printStackTrace();
36 }
37 }
38
39 private DbHelper() throws Exception {
40 connection = DriverManager.getConnection(URL, USER, PASSWORD);
41 statement = connection.createStatement();
42 }
43
44 /**
45 * 返回单例模式的数据库辅助对象
46 *
47 * @return
48 * @throws Exception
49 */
50 public static DbHelper getDbHelper() throws Exception {
51 if (helper == null || connection == null || connection.isClosed())
52 helper = new DbHelper();
53 return helper;
54 }
55
56 /**
57 * 执行查询
58 * @param sql 要执行的SQL语句
59 * @return 查询的结果集对象
60 * @throws Exception
61 */
62 public ResultSet executeQuery(String sql) throws Exception {
63 if (statement != null) {
64 return statement.executeQuery(sql);
65 }
66
67 throw new Exception("数据库未正常连接");
68 }
69
70 /**
71 * 执行查询
72 * @param sql 要执行的带参数的SQL语句
73 * @param args SQL语句中的参数值
74 * @return 查询的结果集对象
75 * @throws Exception
76 */
77 public ResultSet executeQuery(String sql, Object...args) throws Exception {
78 if (connection == null || connection.isClosed()) {
79 DbHelper.close();
80 throw new Exception("数据库未正常连接");
81 }
82 PreparedStatement ps = connection.prepareStatement(sql);
83 int index = 1;
84 for (Object arg : args) {
85 ps.setObject(index, arg);
86 index++;
87 }
88
89 return ps.executeQuery();
90 }
91
92 /**
93 * 执行更新
94 * @param sql 要执行的SQL语句
95 * @return 受影响的记录条数
96 * @throws Exception
97 */
98 public int executeUpdate(String sql) throws Exception {
99 if (statement != null) {
100 return statement.executeUpdate(sql);
101 }
102 throw new Exception("数据库未正常连接");
103 }
104
105 /**
106 * 执行更新
107 * @param sql 要执行的SQL语句
108 * @param args SQL语句中的参数
109 * @return 受影响的记录条数
110 * @throws Exception
111 */
112 public int executeUpdate(String sql, Object...args) throws Exception {
113 if (connection == null || connection.isClosed()) {
114 DbHelper.close();
115 throw new Exception("数据库未正常连接");
116 }
117 PreparedStatement ps = connection.prepareStatement(sql);
118 int index = 1;
119 for (Object arg : args) {
120 ps.setObject(index, arg);
121 index++;
122 }
123 return ps.executeUpdate();
124 }
125
126 /**
127 * 获取预编译的语句对象
128 * @param sql 预编译的语句
129 * @return 预编译的语句对象
130 * @throws Exception
131 */
132 public PreparedStatement prepareStatement(String sql) throws Exception {
133 return connection.prepareStatement(sql);
134 }
135
136 /**
137 * 关闭对象,同时将关闭连接
138 */
139 public static void close() {
140 try {
141 if (statement != null)
142 statement.close();
143 if (connection != null)
144 connection.close();
145 } catch (Exception e) {
146 e.printStackTrace();
147 } finally {
148 helper = null;
149 }
150 }
151 }
JAVA连接、操作数据库的DBHelper的更多相关文章
- java连接操作数据库
Connection 类prepareStatement(String sql) 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库. PreparedState ...
- Java连接MySQL数据库及简单操作代码
1.Java连接MySQL数据库 Java连接MySql需要下载JDBC驱动MySQL-connector-java-5.0.5.zip(举例,现有新版本).然后将其解压缩到任一目录.我是解压到D盘, ...
- Java连接SqlServer2008数据库(转)
Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下 ...
- 转载:Java连接MySQL 数据库的正确操作流程
转载网址:http://www.bitscn.com/pdb/mysql/201005/186551.html 以下的文章主要介绍的是Java连接MySQL 数据库(以MySQL数据库为例 ...
- Java进阶(二十五)Java连接mysql数据库(底层实现)
Java进阶(二十五)Java连接mysql数据库(底层实现) 前言 很长时间没有系统的使用java做项目了.现在需要使用java完成一个实验,其中涉及到java连接数据库.让自己来写,记忆中已无从搜 ...
- Java连接SqlServer2008数据库
Java连接SqlServer2008数据库 首先下载JDBC:下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 下 ...
- java连接MySql数据库 zeroDateTimeBehavior
JAVA连接MySQL数据库,在操作值为0的timestamp类型时不能正确的处理,而是默认抛出一个异常, 就是所见的:java.sql.SQLException: Cannot convert va ...
- JAVA连接Derby数据库
其实,JAVA连接Derby数据库也很简单,和一般的数据库操作一样:1.加载驱动2.获取连接3.进行数据库操作4.记得关闭连接 示例如下: import java.sql.ResultSet; imp ...
- java连接mongodb数据库
最近毕设需要用到这个数据库.然而又不会,没办法,只能上网学习学习. 记录一下java连接mongodb数据库的简单方法.这里只是记录一下学习.熟悉一下CURD方法. 但是毕业用到的是SpringBoo ...
- java连接mysql数据库详细步骤解析
java连接mysql数据库详细步骤解析 第一步:下载一个JDBC驱动包,例如我用的是:mysql-connector-java-5.1.17-bin.jar 第二步:导入下载的J ...
随机推荐
- CRLF注入
CRLF注入 Title: [CVE-2019-9740] Python urllib CRLF injection vulnerability Category: security Stage: r ...
- Python中类的特殊属性和魔术方法
1.属性 属性 含义 __name__ 类.函数.方法等的名字 __dir__ __module__ 类定义所在的模块名 __class__ 对象或类所属的类 只是返回基类 __bases__ ...
- 让你的浏览器变成Siri一样的语音助手
最近业余时间浏览技术文章的时候,看到了一篇关于语音朗读的文章:Use JavaScript to Make Your Browser Speak(用Javascript让你的浏览器说话),文章中提到可 ...
- Blind Super-Resolution Kernel Estimation using an Internal-GAN 论文解读
背景与思路来源 目前 SR 模型中合成 LR 使用的模糊核问题 目前大多数 SR 的 model 都是用的合成下采样图片来进行训练的,而这些合成的图片常常使用的是 MATLAB 里面的 imresiz ...
- Android 系统开发做什么?
题外话 18 年我从 Android 应用开发转 Framework 层开发了,从此开启了 996 幸福生活,博客技术文更新基本停滞了,被工作占据了过多的精力,实在没时间像以前一样拟稿.写作,实践.反 ...
- 以Aliyun体验机为例,从零搭建LNMPR环境(上)
使用云服务器搭建 Web 运行环境,尤其是搭建常见的 LNMPR(Linux+Nginx+MySQL+PHP+Redis) 环境,对于开发人员是必备的职场基本技能之一.在这里,借着搭建我的" ...
- 全面了解Vue3的 reactive 和相关函数
Vue3的 reactive 怎么用,原理是什么,官网上和reactive相关的那些函数又都是做什么用处的?这里会一一解答. ES6的Proxy Proxy 是 ES6 提供的一个可以拦截对象基础操作 ...
- Nginx记录用户请求Header到access log
为了统计和其它用途,经常有人需要自定义Nginx日志,把http请求中的某个字段记录到日志中,刚好在看lua+nginx的文章,第一想到的是用lua赋值来做,但是想想有点小恶心,于是Google了一番 ...
- 学习笔记-ionic3 环境配置搭建到打包
折腾了两周总算理清楚了,参考的链接如下: https://blog.csdn.net/zeternityyt/article/details/79655150 环境配置 https://segmen ...
- 010_Nginx入门
目录 使用场景 什么是Nginx 正向代理与反向代理 正向代理:代理客户端 反向代理:代理服务端 Nginx的作用 反向代理 负载均衡 轮询 加权轮询 IP hash 动静分离 Windows下安装 ...