赞
踩
1.首先先配置一个c3p0-config.xml文件,这个问价就放在src根目录之下即可
-----------------------------------------------
<!-- 自定义的c3p0-config.xml --> <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </default-config> <named-config name="myOwnAdaptation"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/school</property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> </named-config> </c3p0-config>
DataSource ds = new ComboPooledDataSource("myOwnAdaptation ");
参数对应使用哪个config,如果不写,表示使用默认的config,即default-config里的配置,否则使用参数指定的named-config里的配置,这种xml配置的操作即相当于调用了被实例化类对象的setter方法,根据name=”xxx“来对应类中的属性从而实例化了这个对象。
Connection conn = ds.getConnection();
conn.close();
-----------------------------------------------
2、使用c3p0.properties配置文件
c3p0.driverClass = com.mysql.jdbc.Driver
c3p0.jdbcUrl = jdbc:mysql://localhost:3306/happyFactory
c3p0.user = root
c3p0.password =root
c3p0.maxPoolSize = 20
c3p0.minPoolSize = 3
c3p0.maxStatements = 30
c3p0.maxIdleTime = 150
(1)在静态初始化中使用Properties类来加载c3p0.properties,并将读取到的property设置到DataSource上去。
代码如下:
DataSource ds = new ComboPooledDataSource();
Properties props = new Properties();
props.load(类名.class.getClassLoader().getResourceAsStream("c3p0.properties"));
ds.setDriverClass(props.getProperty("driverClass"));
ds.setJdbcUrl(props.getProperty("jdbcUrl"));
ds.setUername(props.getProperty("user"));
ds.setPassword (props.getProperty("password "));
Connection conn = ds.getConnection();
conn.close();
这里再说一下工具类的一般创建(此处是c3p0.properties文件和xml文件卸载了一起,择一即可):
```java public class Utils { /** * 文件读取,只会执行一次,使用静态代码块 */ static { //读取文件,获取值 try { //1.创建Properties集合类 Properties pro = new Properties(); //获取src路径下的文件--->ClassLoader类加载器 ClassLoader classLoader = Utils .class.getClassLoader(); URL resource = classLoader.getResource("c3p0.properties");; String path = resource.getPath(); //2.加载文件 pro.load(new FileReader(path)); //3获取数据 url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); //或者 //DataSource ds = new ComboPooledDataSource("myOwnAdaptation "); //4.注册驱动 Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 获取连接 * @return 连接对象 */ public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(url, user, password); return conn; } /** * 释放资源 * @param rs * @param st * @param conn */ public static void close(ResultSet rs, Statement st,Connection conn){ if (rs != null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。