标签:mysql ... sql ada 函数 package word bpa ack
1:第一种方式很easy
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://localhost:3308/database c3p0.user=root c3p0.password=root
package jdbc.mysql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3P0 { public static void main(String[] args) throws SQLException { //数据库连接池默认会到classpath下去找数据库的配置。ComboPooledDataSource data = new ComboPooledDataSource(); Connection conn = data.getConnection(); String sql = "select * from table"; PreparedStatement pstmt = conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); } } }
依据属性名字差点儿能够看出意思。
<?假设你把c3p0-config.xml放到src文件夹以下,那么你就不须要指定配置文件,它自己主动会找到这个配置文件。即在程序中不用写System.setProperty("com.mchange.v2.c3p0.cfg.xml",System.getProperty("user.dir")+"/config/c3p0-config.xml");这句话。xml version="1.0" encoding="UTF-8"?
> <c3p0-config> <default-config> <property name="user">root</property> <property name="password">1234</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/database?
characterEncoding=UTF-8&useOldAliasMetadataBehavior=true</property> <property name="initialPoolSize">2</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">2</property> </default-config> <named-config name="database"> <property name="user">root</property> <property name="password">root</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3308/database?characterEncoding=UTF-8&useOldAliasMetadataBehavior=true</property> <property name="initialPoolSize">2</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">2</property> </named-config> </c3p0-config>
package jdbc.mysql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class C3p0JDBC { public static void main(String[] args) throws SQLException { System.setProperty("com.mchange.v2.c3p0.cfg.xml",System.getProperty("user.dir")+"/config/c3p0-config.xml"); //上面的配置文件 <default-config> //默认情况下 构造函数传空值。会去找默认配置 DataSource data = new ComboPooledDataSource(); //上面的配置文件 <named-config name="database"> //假设构造函数中传值,则找到相应的配置。 DataSource data1 = new ComboPooledDataSource("database"); Connection conn = data.getConnection(); Connection conn1 = data1.getConnection(); String sql = "select * from table"; PreparedStatement pstmt = conn1.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()){ System.out.println(rs.getString(1)); } } }
可选择性使用
import com.mchange.v2.c3p0.*; ........ ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("dbuser"); cpds.setPassword("dbpassword"); cpds.setMaxStatements( 180 ); cpds.close();
标签:mysql ... sql ada 函数 package word bpa ack
原文地址:http://www.cnblogs.com/jzssuanfa/p/7000845.html