1.c3p0连接数据库由于时区没限制报出的错误
代码:
package C3P0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.log.ModifyLogLevel;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.logging.Level;
public class C3P0_test {
public static Connection getConnection1() throws Exception{
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/library");
cpds.setUser("root");
cpds.setPassword("123456");
// cpds.setMaxPoolSize(100);
Connection conn = cpds.getConnection();
return conn;
}
public static void main(String[] args) throws Exception {
System.out.println(getConnection1());
/*DataSource cpds = new ComboPooledDataSource("helloc3p0");
Connection conn = cpds.getConnection();
System.out.println(conn);*/
}
}
处理方式:需要在url后添加:?serverTimezone=UTC
处理后就能正常执行
八月啥不是在报错是代码执行报出的日志,处理方式在自己对应的项目下创建一个与日志同名的包,代码如下:
创建完成后,在对应的主方法中调用该方法就能完美处理日志(将调用部分放在代码块中优先执行)
遇见版本或字符集或时区问题处理方式:
url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=falser 如果只有其中一个问题取其一即可;如果问题都有全加上就能完美处理问题
2.C3P0采用配置文件连接数据库
如果在对应的xml文件中url后添加:?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=falser时需要添加转义字符:&
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloc3p0">
<!-- 获取连接的4个基本信息 -->
<property name="user">root</property>
<property name="password">123456</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/library?&useSSL=false&serverTimezone=UTC</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 涉及到数据库连接池的管理的相关属性的设置 -->
<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>