|
1 | | -package com.lwl.ems.util; |
2 | | - |
3 | | -import java.io.FileInputStream; |
4 | | -import java.io.IOException; |
5 | | -import java.sql.CallableStatement; |
6 | | -import java.sql.Connection; |
7 | | -import java.sql.DriverManager; |
8 | | -import java.sql.PreparedStatement; |
9 | | -import java.sql.ResultSet; |
10 | | -import java.sql.SQLException; |
11 | | -import java.sql.Statement; |
12 | | -import java.util.Properties; |
13 | | - |
14 | | -public enum ConnectionUtil { |
15 | | - util; |
16 | | - |
17 | | - private static Properties properties; |
18 | | - static { |
19 | | - properties = new Properties(); |
20 | | - try { |
21 | | - //properties.load(Thread.currentThread().getClass().getResourceAsStream("/db.properties")); |
22 | | - properties.load(ConnectionUtil.class.getResourceAsStream("/db.properties")); |
23 | | - |
24 | | - } catch (IOException e) { |
25 | | - e.printStackTrace(); |
26 | | - } |
27 | | - } |
28 | | - |
29 | | - public Connection getConnection() { |
30 | | - Connection con = null; |
31 | | - try { |
32 | | - |
33 | | - con = DriverManager.getConnection(properties.getProperty("url"),properties); |
34 | | - }catch (Exception e) { |
35 | | - System.out.println("While connecting with db:"+e); |
36 | | - } |
37 | | - return con; |
38 | | - |
39 | | - } |
40 | | - public void close(ResultSet rs, Statement st, Connection con) { |
41 | | - try { |
42 | | - if(rs != null) |
43 | | - rs.close(); |
44 | | - if(st!=null) |
45 | | - st.close(); |
46 | | - if(con != null) |
47 | | - con.close(); |
48 | | - }catch (SQLException e) { |
49 | | - System.out.println("While closing resources :"+e); |
50 | | - } |
51 | | - } |
52 | | - public void close(Statement st, Connection con) { |
53 | | - try { |
54 | | - |
55 | | - if(st!=null) |
56 | | - st.close(); |
57 | | - if(con != null) |
58 | | - con.close(); |
59 | | - }catch (SQLException e) { |
60 | | - System.out.println("While closing resources :"+e); |
61 | | - } |
62 | | - } |
63 | | - public void close(ResultSet rs, PreparedStatement pst, Connection con) { |
64 | | - try { |
65 | | - if(rs != null) |
66 | | - rs.close(); |
67 | | - if(pst!=null) |
68 | | - pst.close(); |
69 | | - if(con != null) |
70 | | - con.close(); |
71 | | - }catch (SQLException e) { |
72 | | - System.out.println("While closing resources :"+e); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - public void close(PreparedStatement pst, Connection con) { |
77 | | - try { |
78 | | - |
79 | | - if(pst!=null) |
80 | | - pst.close(); |
81 | | - if(con != null) |
82 | | - con.close(); |
83 | | - }catch (SQLException e) { |
84 | | - System.out.println("While closing resources :"+e); |
85 | | - } |
86 | | - } |
87 | | - public void close(ResultSet rs, CallableStatement cst, Connection con) { |
88 | | - try { |
89 | | - if(rs != null) |
90 | | - rs.close(); |
91 | | - if(cst!=null) |
92 | | - cst.close(); |
93 | | - if(con != null) |
94 | | - con.close(); |
95 | | - }catch (SQLException e) { |
96 | | - System.out.println("While closing resources :"+e); |
97 | | - } |
98 | | - } |
99 | | - public void close(CallableStatement cst, Connection con) { |
100 | | - try { |
101 | | - |
102 | | - if(cst!=null) |
103 | | - cst.close(); |
104 | | - if(con != null) |
105 | | - con.close(); |
106 | | - }catch (SQLException e) { |
107 | | - System.out.println("While closing resources :"+e); |
108 | | - } |
109 | | - } |
110 | | -} |
| 1 | +package com.lwl.ems.util; |
| 2 | + |
| 3 | +import java.sql.CallableStatement; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | + |
| 10 | +import com.zaxxer.hikari.HikariConfig; |
| 11 | +import com.zaxxer.hikari.HikariDataSource; |
| 12 | + |
| 13 | +public enum ConnectionUtil { |
| 14 | + util; |
| 15 | + |
| 16 | + HikariDataSource ds; |
| 17 | + |
| 18 | + { |
| 19 | + HikariConfig config = new HikariConfig(ConnectionUtil.class.getResource("/db.properties").getPath()); |
| 20 | + ds = new HikariDataSource(config); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public Connection getConnection() { |
| 25 | + Connection con = null; |
| 26 | + try { |
| 27 | + return ds.getConnection(); |
| 28 | + } catch (Exception e) { |
| 29 | + System.out.println("While connecting with db:" + e); |
| 30 | + } |
| 31 | + return con; |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + public void close(ResultSet rs, Statement st, Connection con) { |
| 36 | + try { |
| 37 | + if (rs != null) |
| 38 | + rs.close(); |
| 39 | + if (st != null) |
| 40 | + st.close(); |
| 41 | + if (con != null) |
| 42 | + con.close(); |
| 43 | + } catch (SQLException e) { |
| 44 | + System.out.println("While closing resources :" + e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void close(Statement st, Connection con) { |
| 49 | + try { |
| 50 | + |
| 51 | + if (st != null) |
| 52 | + st.close(); |
| 53 | + if (con != null) |
| 54 | + con.close(); |
| 55 | + } catch (SQLException e) { |
| 56 | + System.out.println("While closing resources :" + e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void close(ResultSet rs, PreparedStatement pst, Connection con) { |
| 61 | + try { |
| 62 | + if (rs != null) |
| 63 | + rs.close(); |
| 64 | + if (pst != null) |
| 65 | + pst.close(); |
| 66 | + if (con != null) |
| 67 | + con.close(); |
| 68 | + } catch (SQLException e) { |
| 69 | + System.out.println("While closing resources :" + e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void close(PreparedStatement pst, Connection con) { |
| 74 | + try { |
| 75 | + |
| 76 | + if (pst != null) |
| 77 | + pst.close(); |
| 78 | + if (con != null) |
| 79 | + con.close(); |
| 80 | + } catch (SQLException e) { |
| 81 | + System.out.println("While closing resources :" + e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public void close(ResultSet rs, CallableStatement cst, Connection con) { |
| 86 | + try { |
| 87 | + if (rs != null) |
| 88 | + rs.close(); |
| 89 | + if (cst != null) |
| 90 | + cst.close(); |
| 91 | + if (con != null) |
| 92 | + con.close(); |
| 93 | + } catch (SQLException e) { |
| 94 | + System.out.println("While closing resources :" + e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void close(CallableStatement cst, Connection con) { |
| 99 | + try { |
| 100 | + |
| 101 | + if (cst != null) |
| 102 | + cst.close(); |
| 103 | + if (con != null) |
| 104 | + con.close(); |
| 105 | + } catch (SQLException e) { |
| 106 | + System.out.println("While closing resources :" + e); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments