: Socket closed 或者 该连接已关闭
1 package ;
2
3 import ;
4 import ;
5 import .*;
6 import ;
7 import ;
8 import ;
9 import ;
10 import ;
11 import ;
12 import ;
13
14 import ;
15 import ;
16 import ;
17 import ;
18
19 public class DBUtil {
20 //连接对象
21 //Statement 命令对象
22 //打开连接
23 //关闭连接
24 //得到一个连接对象
25 //查询(有参,无参)
26 //修改(有参,无参)
27 static Statement stmt = null;
28 //驱动,服务器地址,登录用户名,密码
29 static String DBDRIVER;
30 static String DBURL;
31 static String DBUID;
32 static String DBPWD;
33
34 static {
35 //先创建资源文件,扩展名为.properties
36 //内容是以:dbuser=sa 格式
37
38 Properties prop = new Properties();//先获取资源对象
39 //创建输入流,读取资源文件
40 InputStream in =().getContextClassLoader()
41 .getResourceAsStream("");
42 try {
43 (in);//加载
44 DBDRIVER = ("DBDRIVER");
45 DBURL = ("DBURL");
46 DBUID = ("DBUID");
47 DBPWD = ("DBPWD");
48 //(DBDRIVER);
49 } catch (IOException e) {
50 ("资源文件读取错误,请查看资源文件");
51 }
52 try {
53 ();
54 } catch (IOException e) {
55 ();
56 }
57 }
58 //打开连接
59 static {
60 //加载驱动
61 try {
62 (DBDRIVER);
63 } catch (ClassNotFoundException e) {
64 ();
65 }
66 }
67 //关闭连接
68 public static void close(Connection conn) {
69 try {
70 if(stmt!=null)
71 ();
72 if(conn!=null && !())
73 ();
74 } catch (SQLException e) {
75 // TODO Auto-generated catch block
76 ();
77 }
78 }
79 public static void close(ResultSet rs) {
80 Statement st = null;
81 Connection con = null;
82 try {
83 try {
84 if (rs != null) {
85 st = ();
86 ();
87 }
88 } finally {
89 try {
90 if (st != null) {
91 con = ();
92 ();
93 }
94 } finally {
95 if (con != null) {
96 ();
97 }
98 }
99 }
100 } catch (SQLException e) {
101 ();
102 }
103 }
104 //得到一个连接对象,当用户使用DBUtil无法解决个性问题时
105 //可以通过本方法获得连接对象
106 public static Connection getConnection() {
107 Connection conn = null;
108 try {
109 conn=(DBURL,DBUID,DBPWD);
110 } catch (SQLException e) {
111 ();
112 }
113 return conn;
114 }
115
116 //executeQuery
117 //executeUpdate
118 //execute
119 //获得查询的数据集
120 //select * from student where name='' and sex=''
121 public static ResultSet executeQuery(String sql) {
122 Connection conn = getConnection();
123 try {
124 stmt = ();
125 return (sql);
126 } catch (SQLException e) {
127 // TODO Auto-generated catch block
128 ();
129 }
130 return null;
131 }
132
133 //修改表格内容
134 public static int executeUpdate(String sql) {
135 Connection conn = getConnection();
136 int result = 0;
137 try {
138 stmt = ();
139 result = (sql);
140 } catch (SQLException e) {
141 // TODO Auto-generated catch block
142 ();
143 } finally {
144 close(conn);
145 }
146 return result;
147 }
148 //如果执行的查询或存储过程,会返回多个数据集,或多个执行成功记录数
149 //可以调用本方法,返回的结果,
150 //是一个List<ResultSet>或List<Integer>集合
151 public static Object execute(String sql) {
152 Connection conn = getConnection();
153 boolean b=false;
154 try {
155 stmt = ();
156 b = (sql);
157 //true,执行的是一个查询语句,我们可以得到一个数据集
158 //false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
159 if(b){
160 return ();
161 }
162 else {
163 return ();
164 }
165 } catch (SQLException e) {
166 // TODO Auto-generated catch block
167 ();
168 } finally {
169 if(!b) {
170 close(conn);
171 }
172 }
173 return null;
174 }
175
176 //
177 //select * from student where name=? and sex=?
178 public static ResultSet executeQuery(String sql,Object[] in) {
179 Connection conn = getConnection();
180 try {
181 PreparedStatement pst = (sql);
182 for(int i=0;i<;i++)
183 (i+1, in[i]);
184 stmt = pst;//只是为了关闭命令对象pst
185 return ();
186 } catch (SQLException e) {
187 // TODO Auto-generated catch block
188 ();
189 }
190 return null;
191 }
192
193 public static int executeUpdate(String sql,Object[] in) {
194 Connection conn = getConnection();
195 try {
196 PreparedStatement pst = (sql);
197 for(int i=0;i<;i++)
198 (i+1, in[i]);
199 stmt = pst;//只是为了关闭命令对象pst
200 return ();
201 } catch (SQLException e) {
202 // TODO Auto-generated catch block
203 ();
204 }finally {
205 close(conn);
206 }
207 return 0;
208 }
209 public static Object execute(String sql,Object[] in) {
210 Connection conn = getConnection();
211 boolean b=false;
212 try {
213 PreparedStatement pst = (sql);
214 for(int i=0;i<;i++)
215 (i+1, in[i]);
216 b = ();
217 //true,执行的是一个查询语句,我们可以得到一个数据集
218 //false,执行的是一个修改语句,我们可以得到一个执行成功的记录数
219 if(b){
220 ("----");
221 /*List<ResultSet> list = new ArrayList<ResultSet>();
222 (());
223 while(()) {
224 (());
225 }*/
226 return ();
227 }
228 else {
229 ("****");
230 List<Integer> list = new ArrayList<Integer>();
231 (());
232 while(()) {
233 (());
234 }
235 return list;
236 }
237 } catch (SQLException e) {
238 // TODO Auto-generated catch block
239 ();
240 } finally {
241 if(!b) {
242 ("====");
243 close(conn);
244 }
245 }
246 return null;
247 }
248 //调用存储过程 proc_Insert(?,?,?)
249 public static Object executeProcedure(String procName,Object[] in) {
250 Connection conn = getConnection();
251 try {
252 procName = "{call "+procName+"(";
253 String link="";
254 for(int i=0;i<;i++) {
255 procName+=link+"?";
256 link=",";
257 }
258 procName+=")}";
259 CallableStatement cstmt = (procName);
260 for(int i=0;i<;i++) {
261 (i+1, in[i]);
262 }
263 if(())
264 {
265 return ();
266 }
267 else {
268 return ();
269 }
270 } catch (SQLException e) {
271 // TODO Auto-generated catch block
272 ();
273 }
274
275 return null;
276 }
277
278
279 /*
280 * 调用存储过程,并有输出参数
281 * @procName ,存储过程名称:proc_Insert(?,?)
282 * @in ,输入参数集合
283 * @output,输出参数集合
284 * @type,输出参数类型集合
285 * */
286 public static Object executeOutputProcedure(String procName,
287 Object[] in,Object[] output,int[] type){
288 Connection conn = getConnection();
289 Object result = null;
290 try {
291 CallableStatement cstmt = ("{call "+procName+"}");
292 //设置存储过程的参数值
293 int i=0;
294 for(;i<;i++){//设置输入参数
295 (i+1, in[i]);
296 //print(i+1);
297 }
298 int len = +i;
299 for(;i<len;i++){//设置输出参数
300 (i+1,type[i-]);
301 //print(i+1);
302 }
303 boolean b = ();
304 //获取输出参数的值
305 for(i=;i<+;i++)
306 output[] = (i+1);
307 if(b) {
308 result = ();
309 }
310 else {
311 result = ();
312 }
313 } catch (SQLException e) {
314 // TODO Auto-generated catch block
315 ();
316 }
317 return result;
318 }
319 public static String toJson(Object obj){
320 String reuqest=null;
321 //对象映射
322 ObjectMapper mapper=new ObjectMapper();
323 //设置时间格式
324 SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy年MM月dd日");
325 (dateFormat);
326 try {
327 reuqest=(obj);
328 } catch (JsonProcessingException e) {
329 // TODO Auto-generated catch block
330 ();
331 }
332 return reuqest;
333 }
334 public static <T> T toObject(String src,Class<T> valueType){
335 T request=null;
336 //对象反射
337 ObjectMapper mapper=new ObjectMapper();
338 try {
339 request=(src, valueType);
340 } catch (JsonParseException e) {
341 // TODO Auto-generated catch block
342 ();
343 } catch (JsonMappingException e) {
344 // TODO Auto-generated catch block
345 ();
346 } catch (IOException e) {
347 // TODO Auto-generated catch block
348 ();
349 }
350 return request;
351 }
352 public static Date date(String date_str) {
353 try {
354 Calendar zcal = ();//日期类
355 Timestamp timestampnow = new Timestamp(());//转换成正常的日期格式
356 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//改为需要的东西
357 ParsePosition pos = new ParsePosition(0);
358 current = (date_str, pos);
359 timestampnow = new Timestamp(());
360 return timestampnow;
361 }
362 catch (NullPointerException e) {
363 return null;
364 }
365 }
366 }