本人沒有使用任何框架,就是純JAVA的方式css
一、下載Durid包(druid-0.2.23.jar)java
百度網盤:https://pan.baidu.com/s/1q8LsG_Hv1PosCRwOXOiebwweb
密碼:b3cwsql
二、web.xml配置數據庫
<filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
三、工具類DbPoolConnection(獲取connection對象)apache
1 package com.richinfo.dao.base; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.sql.SQLException; 9 import java.util.Properties; 10 11 import com.alibaba.druid.pool.DruidDataSource; 12 import com.alibaba.druid.pool.DruidDataSourceFactory; 13 import com.alibaba.druid.pool.DruidPooledConnection; 14 15 public class DbPoolConnection { 16 private static DbPoolConnection databasePool = null; 17 private static DruidDataSource dds = null; 18 static { 19 Properties properties = loadPropertyFile("durid.properties"); 20 try { 21 dds = (DruidDataSource) DruidDataSourceFactory 22 .createDataSource(properties); 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 private DbPoolConnection() {} 28 public static synchronized DbPoolConnection getInstance() { 29 if (null == databasePool) { 30 databasePool = new DbPoolConnection(); 31 } 32 return databasePool; 33 } 34 public DruidPooledConnection getConnection() throws SQLException { 35 return dds.getConnection(); 36 } 37 public static Properties loadPropertyFile(String fullFile) { 38 String webRootPath = null; 39 if (null == fullFile || fullFile.equals("")) 40 throw new IllegalArgumentException( 41 "Properties file path can not be null : " + fullFile); 42 webRootPath = DbPoolConnection.class.getClassLoader().getResource("") 43 .getPath(); 44 webRootPath = new File(webRootPath).getParent(); 45 InputStream inputStream = null; 46 Properties p = null; 47 try { 48 inputStream = new FileInputStream(new File(webRootPath 49 + File.separator + fullFile)); 50 p = new Properties(); 51 p.load(inputStream); 52 } catch (FileNotFoundException e) { 53 throw new IllegalArgumentException("Properties file not found: " 54 + fullFile); 55 } catch (IOException e) { 56 throw new IllegalArgumentException( 57 "Properties file can not be loading: " + fullFile); 58 } finally { 59 try { 60 if (inputStream != null) 61 inputStream.close(); 62 } catch (IOException e) { 63 e.printStackTrace(); 64 } 65 } 66 return p; 67 } 68 }
四、工具類(對外提供接口,關閉數據庫鏈接)oracle
1 package com.richinfo.dao.base; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import org.apache.logging.log4j.LogManager; 10 import org.apache.logging.log4j.Logger; 11 12 public class DBManager { 13 private static Logger log = LogManager.getLogger(DBManager.class); 14 15 /** 16 * 默認的數據庫連接,連接Oss139 統一位置 管理下的對象 17 * 18 * @return Connection 19 */ 20 public static Connection getConnection() { 21 Connection conn = null; 22 try { 23 conn = DbPoolConnection.getInstance().getConnection(); 24 } catch (SQLException e) { 25 e.printStackTrace(); 26 log.error("DBManager getConnection null.{}", e); 27 return null; 28 } 29 return conn; 30 } 31 32 /** 33 * 連接指定的 數據庫對象 34 * 35 * @param dataBaseName 36 * 對象名 37 * @return Connection 38 */ 39 public static Connection getConnection(String dataBaseName) { 40 41 try { 42 Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 43 } catch (ClassNotFoundException e) { 44 e.printStackTrace(); 45 return null; 46 } catch (Exception e) { 47 e.printStackTrace(); 48 return null; 49 } 50 Connection conn = null; 51 try { 52 conn = DriverManager.getConnection("proxool." + dataBaseName); 53 } catch (SQLException e) { 54 e.printStackTrace(); 55 return null; 56 } 57 return conn; 58 } 59 60 public static void close(Connection conn, Statement st, ResultSet rs) { 61 62 try { 63 if (rs != null) 64 rs.close(); 65 } catch (Exception e) { 66 log.error("DBManager close fail rs. {}", e); 67 } 68 ; 69 try { 70 if (st != null) 71 st.close(); 72 } catch (Exception e) { 73 log.error("DBManager close fail st. {}", e); 74 } 75 ; 76 try { 77 if (conn != null) 78 conn.close(); 79 } catch (Exception e) { 80 log.error("DBManager close fail conn. {}", e); 81 } 82 } 83 }
五、配置文件durid.propertiesapp
driverClassName=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@192.168.9.73:1521:oss username=upm password=upm filters=stat initialSize=2 maxActive=300 maxWait=60000 timeBetweenEvictionRunsMillis=60000 minEvictableIdleTimeMillis=300000 validationQuery=select CURRENT_DATE FROM dual testWhileIdle=true testOnBorrow=false testOnReturn=false poolPreparedStatements=false maxPoolPreparedStatementPerConnectionSize=200