码迷,mamicode.com
首页 > 数据库 > 详细

MySql数据库再学习——使用强化版的自定义连接池连接数据库

时间:2019-07-20 17:18:12      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:连接池   release   value   inf   主程   object   div   iso   dstat   

一:两个重点,

一是获取properties文件的路径,总结在注释里,我当时获取了好半天,总是找不到文件。

二是理清这几个class之间的联系,以及本次demo的思路逻辑关系总的来说就是,在原有的driverManager获取的基础上——>创建一个池子(池子中存储若干个基础的Connection对象)

——>书写一个强化类实现Connection接口(强化的是close()方法)——>在主程序中调用的时候 调用的是从池子中取出来的,强化后的 Connection对象。

dbUtil.class

 1 public class dbUtil {
 2 
 3     /*初始化需要用到的变量*/
 4     private static Connection conn;
 5     private static PreparedStatement pst;
 6     private static ResultSet rs;
 7 
 8     private static String driver;
 9     private static String url;
10     private static String username;
11     private static String password;
12     private static InputStream in;
13     private static ClassLoader cl;
14 
15     static {
16         try {
17             //获取当前类的类加载器
18             //cl = Thread.currentThread().getContextClassLoader();
19 
20             //获取配置文件的输入流
21             // in = cl.getResourceAsStream("db.properties");
22 
23             //切忌使用中文命名文件夹,否则dbUtil.class.getResource("/db.properties").getFile()会找不到文件路径
24             FileInputStream in = new FileInputStream(dbUtil.class.getResource("/db.properties").getFile());
25 
26             //创建一个properties文件对象
27             Properties ppt = new Properties();
28             //加载输入流
29             ppt.load(in);
30             //获得相关参数
31             driver = ppt.getProperty("driver");
32             url = ppt.getProperty("url");
33             username = ppt.getProperty("username");
34             password = ppt.getProperty("password");
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38     }
39 
40 
41     public static Connection getConnection() throws ClassNotFoundException, SQLException {
42         //注册驱动
43         Class.forName(driver);
44         //连接数据库
45         Connection connection = DriverManager.getConnection(url, username, password);
46         return connection;
47     }
48 
49 
50     public static void release(Connection conn, PreparedStatement pst, ResultSet rs) {
51         if (rs != null) {
52             try {
53                 rs.close();
54             } catch (Exception e) {
55                 e.printStackTrace();
56             }
57         }
58         if (pst != null) {
59             try {
60                 pst.close();
61             } catch (Exception e) {
62                 e.printStackTrace();
63             }
64         }
65         if (conn != null) {
66             try {
67                 conn.close();
68             } catch (Exception e) {
69                 e.printStackTrace();
70             }
71         }
72     }
73 
74     @Test
75     public void test() {
76 
77         String sql = "select * from test where name=?";
78         LinkPool pool = new LinkPool();
79         try {
80             //conn=getConnection();
81             //这个链接是从池子里取的,此时尺子中的连接是强化过得 战斗力翻倍.:)
82             conn = pool.getConnection();
83             //这个prepareStatement方法也是重写过得 否则重写过的conn是得不到该方法的
84             pst = conn.prepareStatement(sql);
85             pst.setString(1, "张浩");
86             rs = pst.executeQuery();
87             while (rs.next()) {
88                 System.out.println(rs.getString("name"));
89             }
90         } catch (Exception e) {
91             e.printStackTrace();
92         } finally {
93             //release对象中的close方法也是重写过的
94             release(conn, pst, rs);
95         }
96     }
97 }

 

LinkPool.class

 1 public class LinkPool implements DataSource {
 2         //定义一个池子对象
 3     private static LinkedList<Connection> pool = new LinkedList<>();
 4 
 5     static {
 6         for (int i = 0; i < 5; i++) {
 7             try {
 8                 pool.add(new MyDbConnection(dbUtil.getConnection(), pool));
 9             } catch (ClassNotFoundException e) {
10                 e.printStackTrace();
11             } catch (SQLException e) {
12                 e.printStackTrace();
13             }
14         }
15     }
16 
17 
18     @Override
19     public Connection getConnection() throws SQLException {
20 
21         if (pool.size() == 0) {
22             for (int i = 0; i < 5; i++) {
23                 try {
24                     //想池子中添加Myconnection对象
25                     pool.add(new MyDbConnection(dbUtil.getConnection(), pool));
26                 } catch (ClassNotFoundException e) {
27                     e.printStackTrace();
28                 } catch (SQLException e) {
29                     e.printStackTrace();
30                 }
31             }
32         }
33         return pool.remove(0);
34     }
35 
36     @Override
37     public Connection getConnection(String username, String password) throws SQLException {
38         return null;
39     }
40 
41     @Override
42     public <T> T unwrap(Class<T> iface) throws SQLException {
43         return null;
44     }
45 
46     @Override
47     public boolean isWrapperFor(Class<?> iface) throws SQLException {
48         return false;
49     }
50 
51     @Override
52     public PrintWriter getLogWriter() throws SQLException {
53         return null;
54     }
55 
56     @Override
57     public void setLogWriter(PrintWriter out) throws SQLException {
58 
59     }
60 
61     @Override
62     public void setLoginTimeout(int seconds) throws SQLException {
63 
64     }
65 
66     @Override
67     public int getLoginTimeout() throws SQLException {
68         return 0;
69     }
70 
71     @Override
72     public Logger getParentLogger() throws SQLFeatureNotSupportedException {
73         return null;
74     }
75 }

MyDbConnection.class

  1 public class MyDbConnection implements Connection {
  2     //为了通过构造方法 将普通的connection对象传进来,然后对close方法进行强化
  3     //pool对象也是如此
  4     private Connection conn;
  5     private LinkedList<Connection> pool;
  6 
  7     public MyDbConnection(Connection conn, LinkedList<Connection> pool) {
  8         this.conn = conn;
  9         this.pool = pool;
 10     }
 11 
 12     @Override
 13     public void close() throws SQLException {
 14         //实现pool的对象补充
 15         pool.add(conn);
 16     }
 17 
 18     @Override
 19     public PreparedStatement prepareStatement(String sql) throws SQLException {
 20         //重写prepareStatement方法
 21         return conn.prepareStatement(sql);
 22     }
 23 
 24     @Override
 25     public Statement createStatement() throws SQLException {
 26         return null;
 27     }
 28 
 29 
 30     @Override
 31     public CallableStatement prepareCall(String sql) throws SQLException {
 32         return null;
 33     }
 34 
 35     @Override
 36     public String nativeSQL(String sql) throws SQLException {
 37         return null;
 38     }
 39 
 40     @Override
 41     public void setAutoCommit(boolean autoCommit) throws SQLException {
 42 
 43     }
 44 
 45     @Override
 46     public boolean getAutoCommit() throws SQLException {
 47         return false;
 48     }
 49 
 50     @Override
 51     public void commit() throws SQLException {
 52 
 53     }
 54 
 55     @Override
 56     public void rollback() throws SQLException {
 57 
 58     }
 59 
 60 
 61     @Override
 62     public boolean isClosed() throws SQLException {
 63         return false;
 64     }
 65 
 66     @Override
 67     public DatabaseMetaData getMetaData() throws SQLException {
 68         return null;
 69     }
 70 
 71     @Override
 72     public void setReadOnly(boolean readOnly) throws SQLException {
 73 
 74     }
 75 
 76     @Override
 77     public boolean isReadOnly() throws SQLException {
 78         return false;
 79     }
 80 
 81     @Override
 82     public void setCatalog(String catalog) throws SQLException {
 83 
 84     }
 85 
 86     @Override
 87     public String getCatalog() throws SQLException {
 88         return null;
 89     }
 90 
 91     @Override
 92     public void setTransactionIsolation(int level) throws SQLException {
 93 
 94     }
 95 
 96     @Override
 97     public int getTransactionIsolation() throws SQLException {
 98         return 0;
 99     }
100 
101     @Override
102     public SQLWarning getWarnings() throws SQLException {
103         return null;
104     }
105 
106     @Override
107     public void clearWarnings() throws SQLException {
108 
109     }
110 
111     @Override
112     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
113         return null;
114     }
115 
116     @Override
117     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
118         return null;
119     }
120 
121     @Override
122     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
123         return null;
124     }
125 
126     @Override
127     public Map<String, Class<?>> getTypeMap() throws SQLException {
128         return null;
129     }
130 
131     @Override
132     public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
133 
134     }
135 
136     @Override
137     public void setHoldability(int holdability) throws SQLException {
138 
139     }
140 
141     @Override
142     public int getHoldability() throws SQLException {
143         return 0;
144     }
145 
146     @Override
147     public Savepoint setSavepoint() throws SQLException {
148         return null;
149     }
150 
151     @Override
152     public Savepoint setSavepoint(String name) throws SQLException {
153         return null;
154     }
155 
156     @Override
157     public void rollback(Savepoint savepoint) throws SQLException {
158 
159     }
160 
161     @Override
162     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
163 
164     }
165 
166     @Override
167     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
168         return null;
169     }
170 
171     @Override
172     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
173         return null;
174     }
175 
176     @Override
177     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
178         return null;
179     }
180 
181     @Override
182     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
183         return null;
184     }
185 
186     @Override
187     public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
188         return null;
189     }
190 
191     @Override
192     public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
193         return null;
194     }
195 
196     @Override
197     public Clob createClob() throws SQLException {
198         return null;
199     }
200 
201     @Override
202     public Blob createBlob() throws SQLException {
203         return null;
204     }
205 
206     @Override
207     public NClob createNClob() throws SQLException {
208         return null;
209     }
210 
211     @Override
212     public SQLXML createSQLXML() throws SQLException {
213         return null;
214     }
215 
216     @Override
217     public boolean isValid(int timeout) throws SQLException {
218         return false;
219     }
220 
221     @Override
222     public void setClientInfo(String name, String value) throws SQLClientInfoException {
223 
224     }
225 
226     @Override
227     public void setClientInfo(Properties properties) throws SQLClientInfoException {
228 
229     }
230 
231     @Override
232     public String getClientInfo(String name) throws SQLException {
233         return null;
234     }
235 
236     @Override
237     public Properties getClientInfo() throws SQLException {
238         return null;
239     }
240 
241     @Override
242     public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
243         return null;
244     }
245 
246     @Override
247     public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
248         return null;
249     }
250 
251     @Override
252     public void setSchema(String schema) throws SQLException {
253 
254     }
255 
256     @Override
257     public String getSchema() throws SQLException {
258         return null;
259     }
260 
261     @Override
262     public void abort(Executor executor) throws SQLException {
263 
264     }
265 
266     @Override
267     public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
268 
269     }
270 
271     @Override
272     public int getNetworkTimeout() throws SQLException {
273         return 0;
274     }
275 
276     @Override
277     public <T> T unwrap(Class<T> iface) throws SQLException {
278         return null;
279     }
280 
281     @Override
282     public boolean isWrapperFor(Class<?> iface) throws SQLException {
283         return false;
284     }
285 }

 

MySql数据库再学习——使用强化版的自定义连接池连接数据库

标签:连接池   release   value   inf   主程   object   div   iso   dstat   

原文地址:https://www.cnblogs.com/zhang188660586/p/11218133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!