1:第一种方式非常简单 c3p0.driverClass=com.mysql.jdbc.Driverc3p0.jdbcUrl=jdbc:mysql://localhost:3308/databasec3p0.user=rootc3p0.password=root 文件名字为:c3p0.properties(放在src目录下面) //程序我写的比较简单,可以用来测试执行配置。 packa
1:第一种方式非常简单
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));
}
}
}
<?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));
}
}
}可选择性使用
系统特点:功能简洁实用。目前互联网上最简洁的企业网站建设系统!原创程序代码。非网络一般下载后修改的代码。更安全。速度快!界面模版分离。原创的分离思路,完全不同于其他方式,不一样的简单感受!搜索引擎优化。做了基础的seo优化。对搜索引擎更友好系统功能关于我们:介绍企业介绍类信息,可自由添加多个介绍栏目!资讯中心:公司或行业资讯类内容展示。可自由添加多个资讯内容!产品展示:支持类别设置,可添加产品图片
0
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();
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号