1
package lbc;
2
3
import java.sql.Connection;
4
import java.sql.DriverManager;
5
import java.sql.PreparedStatement;
6
import java.sql.ResultSet;
7
import java.sql.ResultSetMetaData;
8
import java.sql.SQLException;
9
import java.sql.Statement;
10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
15
public
final
class SqlHelper {
16
17
private SqlHelper() {
18
19 }
20
21
static {
22
//
加载驱动
23
try {
24 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
25 }
catch (ClassNotFoundException e) {
26 e.printStackTrace();
27
throw
new RuntimeException(e);
28 }
29 }
30
31
private
static Connection getConnection(String connectionString)
throws SQLException {
32 Connection connection = DriverManager.getConnection(connectionString);
33
return connection;
34 }
35
36
private
static
void close(Connection connection,Statement statement) {
37
try {
38
if(connection !=
null){
39 connection.close();
40 }
41
if (statement !=
null) {
42 statement.close();
43 }
44 }
catch (Exception e) {
45 e.printStackTrace();
46 }
47 }
48
49
private
static
void close(ResultSet rs){
50
try {
51
if (rs !=
null) {
52 rs.close();
53 }
54 }
catch (Exception e) {
55 e.printStackTrace();
56 }
57 }
58
59
private
static List<Map<String, Object>> convert(ResultSet rs)
throws SQLException {
60 List<Map<String, Object>> list =
new ArrayList<>();
61
if(rs !=
null && !rs.isClosed()){
62 ResultSetMetaData meta = rs.getMetaData();
63
int colCount=meta.getColumnCount();
64
while (rs.next()) {
65 Map<String, Object> map =
new HashMap<String, Object>();
66
for(
int i=1;i<=colCount;i++){
67 String key = meta.getColumnName(i);
68 Object value = rs.getObject(i);
69 map.put(key, value);
70 }
71 list.add(map);
72
73 }
74 }
75
return list;
76 }
77
78
private
static
void AttachParameters(PreparedStatement statement,Object ...values)
throws Exception {
79
if (values !=
null) {
80
for (
int i = 0; i < values.length; i++) {
81 statement.setObject(i + 1, values[i]);
82 }
83 }
84 }
85
86
public
static
int ExecuteNonQuery(String sql,String connectionString,Object ...parameterValues)
throws SQLException,Exception {
87 Connection connection =
null;
88 PreparedStatement statement =
null;
89
int result = 0;
90
try {
91 connection = getConnection(connectionString);
92 statement = connection.prepareStatement(sql);
93 AttachParameters(statement, parameterValues);
94 result = statement.executeUpdate();
95 statement.clearParameters();
96 }
97
finally {
98 close(connection, statement);
99 }
100
return result;
101 }
102
103
public
static Object ExecuteScalar(String sql,String connectionString,Object ...parameterValues)
throws Exception {
104 ResultSet rs =
null;
105 Object value =
null;
106
try {
107 rs = ExecuteResultSet(sql,connectionString,parameterValues);
108
if(rs !=
null){
109 rs.next();
110 value = rs.getObject(1);
111 }
112 }
113
finally {
114 close(rs);
115 }
116
117
return value;
118 }
119
120
public
static ResultSet ExecuteResultSet(String sql,String connectionString,Object ...parameterValues)
throws Exception {
121 Connection connection =
null;
122 PreparedStatement statement =
null;
123 ResultSet rs =
null;
124
try {
125 connection = getConnection(connectionString);
126 statement = connection.prepareStatement(sql);
127 AttachParameters(statement, parameterValues);
128 rs = statement.executeQuery();
129 statement.clearParameters();
130 }
131
catch (Exception e) {
132 close(connection,statement);
133
throw e;
134 }
135
return rs;
136 }
137
138
public
static List<Map<String, Object>> ExecuteList(String sql,String connectionString,Object ...parameterValues)
throws Exception {
139 ResultSet rs =
null;
140 List<Map<String, Object>> list =
new ArrayList<Map<String, Object>>();
141
try {
142 rs = ExecuteResultSet(sql, connectionString,parameterValues);
143 list = convert(rs);
144 }
145
finally {
146 close(rs);
147 }
148
return list;
149 }
150
151
152 }