01 | package com.sxt.gmms.dao; |
02 |
03 | import java.sql.Connection; |
04 | import java.sql.DriverManager; |
05 | import java.sql.ResultSet; |
06 | import java.sql.Statement; |
07 |
08 | /** |
09 | * 数据库工具类 |
10 | * @author ming |
11 | * |
12 | */ |
13 | public class DBUtil { |
14 | |
15 | /** |
16 | * 公用的一个普通的增删改的执行方法 |
17 | * @param sql |
18 | * @return |
19 | */ |
20 | public static int executeUpdate(String sql){ |
21 | |
22 | Connection con= null ; |
23 | Statement stat= null ; |
24 | int rowCount= 0 ; |
25 | try { |
26 | con=getConn(); |
27 | stat=con.createStatement(); |
28 | rowCount=stat.executeUpdate(sql); |
29 | } catch (Exception e) { |
30 | e.printStackTrace(); |
31 | } finally { |
32 | closeConn(con, stat, null ); |
33 | } |
34 | return rowCount; |
35 | } |
36 | |
37 | /** |
38 | * 专用于取得数据库连接 |
39 | * @return 数据库连接 |
40 | */ |
41 | public static Connection getConn(){ |
42 | Connection con= null ; |
43 | try { |
44 | Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); |
45 | String url = "jdbc:sqlserver://localhost:1433; DatabaseName=esmsdb" ; |
46 | con = DriverManager.getConnection(url, "sa" , "serical" ); |
47 | } catch (Exception e) { |
48 | e.printStackTrace(); |
49 | } |
50 | return con; |
51 | } |
52 | |
53 | /** |
54 | * 专用于关闭数据库连接 |
55 | * @param con |
56 | * @param stat |
57 | * @param rs |
58 | */ |
59 | public static void closeConn(Connection con,Statement stat,ResultSet rs){ |
60 | try { |
61 | if (rs!= null ){ |
62 | rs.close(); |
63 | } |
64 | if (stat!= null ){ |
65 | stat.close(); |
66 | } |
67 | if (con!= null ){ |
68 | con.close(); |
69 | } |
70 | } catch (Exception e) { |
71 | e.printStackTrace(); |
72 | } |
73 | } |
74 |
75 | } |

DoustrongWU LV5
2月12日
胡棋俊 LV4
2024年11月17日
ruifeng LV4
2024年3月3日
LTPR66 LV1
2023年12月1日
1145304128 LV12
2023年5月20日
CL200228 LV4
2023年4月15日
Seem丶君 LV3
2022年11月22日
计算机暴龙战士 LV19
2022年11月19日
srl2881552 LV10
2022年9月5日
youwuzuichen LV11
2022年8月17日