本人没有使用任何框架,就是纯JAVA的方式
1、下载Durid包(druid-0.2.23.jar)
百度网盘:https://pan.baidu.com/s/1q8LsG_Hv1PosCRwOXOiebw
密码:b3cw
2、web.xml配置
DruidWebStatFilter com.alibaba.druid.support.http.WebStatFilter exclusions *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/* DruidWebStatFilter /* DruidStatView com.alibaba.druid.support.http.StatViewServlet DruidStatView /druid/*
3、工具类DbPoolConnection(获取connection对象)
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) DruidDataSourceFactory22 .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(webRootPath49 + 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 }
4、工具类(对外提供接口,关闭数据库连接)
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 Connection19 */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 dataBaseName36 * 对象名37 * @return Connection38 */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 }
5、配置文件durid.properties
driverClassName=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@192.168.9.73:1521:ossusername=upmpassword=upmfilters=statinitialSize=2maxActive=300maxWait=60000timeBetweenEvictionRunsMillis=60000minEvictableIdleTimeMillis=300000validationQuery=select CURRENT_DATE FROM dualtestWhileIdle=truetestOnBorrow=falsetestOnReturn=falsepoolPreparedStatements=falsemaxPoolPreparedStatementPerConnectionSize=200