package com.fgkj.db; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.fgkj.dao.DAOHelper; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBUtil { private static ComboPooledDataSource mysql_ds = new ComboPooledDataSource(); private static String classDriver="com.mysql.jdbc.Driver"; //private static String burl="jdbc:mysql://192.168.0.34:5306/"; //private static String username="website"; //private static String password="amtb3737"; private static String username="root"; private static String password="lmx8688139"; private static String url="jdbc:mysql://118.89.139.230:3360/db_user"; //private static String url="jdbc:mysql://123.207.82.239:3360/db_user"; //private static String url="jdbc:mysql://192.168.7.34:3360/db_user"; //private static String url="jdbc:mysql://127.0.0.1:3360/db_user"; /*private static String username="root"; private static String password="123456";*/ private static int conncount_max = 500; private static int conncount_min=2; private static int MaxStatements = 0; private static int MaxIdleTime=60; private static int InitialPoolSize=2; static{ try { //Thread.sleep(1000*60); mysql_ds.setDriverClass(classDriver); mysql_ds.setUser(username); mysql_ds.setPassword(password); mysql_ds.setMaxPoolSize(conncount_max); mysql_ds.setMinPoolSize(conncount_min); //mysql_ds.setIdleConnectionTestPeriod(60); mysql_ds.setJdbcUrl(url); // 设置连接池中的最大Statements数量! mysql_ds.setMaxStatements(MaxStatements); // 设置连接池的最大空闲时间! mysql_ds.setMaxIdleTime(MaxIdleTime); // 设置初始连接池的大小! mysql_ds.setInitialPoolSize(InitialPoolSize); mysql_ds.setIdleConnectionTestPeriod(60); //mysql_ds.setPreferredTestQuery("SELECT 1"); //mysql_ds.setIdleConnectionTestPeriod(1800); //mysql_ds.setMaxIdleTime(25000); //mysql_ds.setAcquireRetryDelay(100); //mysql_ds.setBreakAfterAcquireFailure(false); //当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒,默认为0 //mysql_ds.setCheckoutTimeout(10000); //因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 //时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable //等方法来提升连接测试的性能。Default: false //mysql_ds.setTestConnectionOnCheckin(false); //如果设置为true,每次从池中取一个连接,将做一下测试,使用automaticTestTable 或者 preferredTestQuery,做一条查询语句.看看连接好不好用,不好用,就关闭它,重新从池中拿一个. //mysql_ds.setTestConnectionOnCheckout(true); /*ArrayList view_list=new ArrayList(); view_list.add(DaoView.db_alarm_view_baoji);//创建包机组视图 try { DAOHelper.makeManualCommit(DBUtil.getConn(), view_list); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("创建触发器/视图报错!"); }*/ } catch (Exception e) { e.printStackTrace(); } } public static Connection getConn(){ /*//System.out.println(DBUtil.getUrl()); Connection conn=null; try { Class.forName(classDriver); conn=DriverManager.getConnection(url+databaseName, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { }*/ Connection conn = null; try { conn = mysql_ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; } //获取当前连接数 public static void getConnections(){ try { //System.out.println("最大连接数: "+mysql_ds.getMaxPoolSize());// 最大连接数 //System.out.println("最小连接数: "+mysql_ds.getMinPoolSize());// 最小连接数 System.out.println("正在使用连接数: "+mysql_ds.getNumBusyConnections());// 正在使用连接数 System.out.println("空闲连接数: "+mysql_ds.getNumIdleConnections());// 空闲连接数 //System.out.println("总连接数: "+mysql_ds.getNumConnections());// 总连接数 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //设置IP地址 public static void setUrl(String ip) { DBUtil.url = "jdbc:mysql://"+ip+":5306"; //System.out.println(DBUtil.url); } public static String getUrl() { return url; } //关闭资源 public static void close(ResultSet rs,Statement stat,Connection conn){ try { if(rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(stat != null) stat.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /** * 初始化连接池 */ public static void initPool(){ close(null, null, getConn()); } public static void main(String[] args){ //System.out.println("start..."); //System.out.println(getConn()); //System.out.println("end..."); Connection conn=DBUtil.getConn(); DBUtil.getConnections(); DBUtil.close(null, null, conn); while(true){ DBUtil.getConnections(); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }