Java interface or abstract class by calling methods of the time, how do you know which implementation class method call is?

With the object  getClass() method to obtain its class, then you can feel free to go to determine which is which implementation of the class.

JDBC connection pool of the code, we want to know is to call the close method conn release connections or connection, such as the return code shown 1-1

We can print conn.getClass () ::

Connection conn = JDBCUtils.getConnection();
System.out.println(conn.getClass());

The results for the class com.alibaba.druid.pool.DruidPooledConnection, so this Connection implementation class is actually DruidPooledConnection, view the source code to know DruidPooledConnection close method is returned to the connection pool, instead of releasing the connection.

 

 

 

 

 

Code 1-1:

 1 package cn.itcast.utils;
 2 
 3 import com.alibaba.druid.pool.DruidDataSourceFactory;
 4 
 5 import javax.sql.DataSource;
 6 import java.io.IOException;
 7 import java.sql.Connection;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.util.Properties;
12 
13 public class JDBCUtils {
14     private static DataSource ds;
15 
16     static{
17         Properties pro = new Properties();
18         try {
19             // 1.加载配置文件
20             pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
21             // 2.获取DataSource
22             ds = DruidDataSourceFactory.createDataSource(pro);
23         } catch (IOException e) {
24             e.printStackTrace();
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28     }
29 
30 
31     public static DataSource getDataSource(){
32         return ds;
33     }
34     /**
35      * 获取连接
36      * @return
37      * @throws SQLException
38      */
39     public static Connection getConnection() throws SQLException {
40         return ds.getConnection();
41     }
42 
43 
44     public static void close(Statement stmt, Connection conn){
45         if(stmt != null){
46             try {
47                 stmt.close();
48             } catch (SQLException e) {
49                 e.printStackTrace();
50             }
51         }
52 
53         if(conn != null){
54             try {
55                 conn.close();
56             } catch (SQLException e) {
57                 e.printStackTrace();
58             }
59         }
60     }
61 
62 
63     public static void close(ResultSet rs, Statement stmt, Connection conn) {
64         if (rs != null) {
65             try {
66                 rs.close();
67             } catch (SQLException e) {
68                 e.printStackTrace();
69             }
70         }
71         if (stmt != null) {
72             try {
73                 stmt.close();
74             } catch (SQLException e) {
75                 e.printStackTrace();
76             }
77         }
78 
79         if (conn != null) {
80             try {
81                 conn.close();
82             } catch (SQLException e) {
83                 e.printStackTrace();
84             }
85         }
86     }
87 
88 
89 }

 

Guess you like

Origin www.cnblogs.com/FengZeng666/p/11610825.html