Java使用JDBC连接SQLServer数据库(二)

将连接数据库、关闭数据库、增删改查数据等对数据库的操作封装成操作数据库的一个类,方便进行数据库的操作。

连接: Java使用JDBC连接SQLServer数据库(一)

一、源码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.operationdb;

import java.sql.*;
/**
* 操作数据库的类,连接SQLserver数据库,以及对数据库的增删改查操作
* @author HuDongyang
*
*/
public class OperationDB {
//驱动路径
private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//数据库地址
private String DBURL = "jdbc:sqlserver://localhost:1434;DataBaseName=";
//数据库登录用户名
private static final String DBUSER = "sa";
//数据库用户密码
private static final String DBPASSWORD = "123456";
//数据库连接
public Connection conn = null;
//执行SQL语句的接口
public Statement stmt = null;
//要执行的SQL语句
public String SQLStr = null;
//数据容器
public ResultSet rs = null;
//提示信息
public String TempInfo = "";


//构造方法
public OperationDB(String DBName) {
this.DBURL += DBName;
}


/**
* 连接数据库
* @return 成功返回true,失败返回false
*/
public boolean linkDB(){
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt = conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return false;
}
}

/**
* 关闭数据库连接
* @return 成功返回true,失败返回false
*/
public boolean closeDB(){
try {
stmt.close();
conn.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}


/**
* 添加数据
* @param TableName 要操作的数据库表名
* @param InsertValue 要增加的数据
* @param WhereStr 查询表中是否已存在要新增的信息
* @return 成功返回true,失败返回false
*/
public boolean insertData(String TableName, String InsertValue, String WhereStr){
this.SQLStr = "insert into " + TableName + " values (" + InsertValue + ")";
try {
if(stmt.executeQuery("select * from " + TableName + " where " + WhereStr).next()){
TempInfo = "该记录已存在!";
this.closeDB();
return false;
}else{
stmt.executeUpdate(SQLStr);
this.closeDB();
return true;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}

}

/**
* 删除数据
* @param TableName 要操作的数据库表名
* @param WhereStr 删除哪一条记录
* @return 成功返回true,失败返回false
*/
public boolean deleteData(String TableName, String WhereStr){
this.SQLStr = "delete from " + TableName + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "数据不存在!";
return false;
}
} catch (SQLException e) {
e.printStackTrace();
this.closeDB();
return false;
}
}

/**
* 修改数据
* @param TableName 要操作的数据库表名
* @param UpdateValue 要修改的属性
* @param WhereStr 要修改哪一条记录
* @return 成功返回true,失败返回false
*/
public boolean updateData (String TableName, String UpdateValue, String WhereStr){
this.SQLStr = "update " + TableName + " set " + UpdateValue + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "数据不存在!";
return false;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}

/**
* 查询数据,该方法没有关闭数据库连接,因为不能关闭RS结果集
* @param TableName 要操作的数据库表名
* @param QueryValue 需要查询的数据
* @param WhereStr 查询哪一条记录
* @return 返回一个ResultSet集合
*/
public ResultSet queryData(String TableName, String QueryValue, String WhereStr){
if(WhereStr == ""){
this.SQLStr = "select "+ QueryValue + " from " + TableName;
}else{
this.SQLStr = "select "+ QueryValue + " from " + TableName + " where " + WhereStr;
}
try {
rs = stmt.executeQuery(SQLStr);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}

}

二、详细说明

1、构造方法

1
2
3
4
//构造方法
public OperationDB(String DBName) {
this.DBURL += DBName;
}

构造方法中,初始化数据库地址;不提供无参构造方法。

2、连接数据库方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   /**
* 连接数据库
* @return 成功返回true,失败返回false
*/
public boolean linkDB(){
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt = conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
try {
stmt.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return false;
}
}

该方法加载数据库驱动;根据之前类中的“数据库地址”、“数据库登录用户名”、“数据库登录密码”,初始化Connection对象;使用Connection的对象conn实例化Statement接口。

3、关闭数据库连接方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   /**
* 关闭数据库连接
* @return 成功返回true,失败返回false
*/
public boolean closeDB(){
try {
stmt.close();
conn.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

4、添加数据方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 添加数据
* @param TableName 要操作的数据库表名
* @param InsertValue 要增加的数据
* @param WhereStr 查询表中是否已存在要新增的信息
* @return 成功返回true,失败返回false
*/
public boolean insertData(String TableName, String InsertValue, String WhereStr){
this.SQLStr = "insert into " + TableName + " values (" + InsertValue + ")";
try {
if(stmt.executeQuery("select * from " + TableName + " where " + WhereStr).next()){
TempInfo = "该记录已存在!";
this.closeDB();
return false;
}else{
stmt.executeUpdate(SQLStr);
this.closeDB();
return true;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}

}

5、删除数据方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 删除数据
* @param TableName 要操作的数据库表名
* @param WhereStr 删除哪一条记录
* @return 成功返回true,失败返回false
*/
public boolean deleteData(String TableName, String WhereStr){
this.SQLStr = "delete from " + TableName + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "数据不存在!";
return false;
}
} catch (SQLException e) {
e.printStackTrace();
this.closeDB();
return false;
}
}

6、修改数据方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 修改数据
* @param TableName 要操作的数据库表名
* @param UpdateValue 要修改的属性
* @param WhereStr 要修改哪一条记录
* @return 成功返回true,失败返回false
*/
public boolean updateData (String TableName, String UpdateValue, String WhereStr){
this.SQLStr = "update " + TableName + " set " + UpdateValue + " where " + WhereStr;
try {
if(stmt.executeUpdate(SQLStr) != 0){
this.closeDB();
return true;
}else{
this.closeDB();
TempInfo = "数据不存在!";
return false;
}
} catch (Exception e) {
e.printStackTrace();
this.closeDB();
return false;
}
}

7、查询数据方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	/**
* 查询数据,该方法没有关闭数据库连接,因为RS结果集关闭后无法使用
* @param TableName 要操作的数据库表名
* @param QueryValue 需要查询的数据
* @param WhereStr 查询哪一条记录
* @return 返回一个ResultSet集合
*/
public ResultSet queryData(String TableName, String QueryValue, String WhereStr){
if(WhereStr == ""){
this.SQLStr = "select "+ QueryValue + " from " + TableName;
}else{
this.SQLStr = "select "+ QueryValue + " from " + TableName + " where " + WhereStr;
}
try {
rs = stmt.executeQuery(SQLStr);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}

}
文章作者: DongyangHu
文章链接: http://hudongyang.com/2019/11/29/jdbc-sqlserver-2/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Sunshine