import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class demo11 {
public static void main(String[] args) throws Exception {
//1.创建c3p0连接池对象:
ComboPooledDataSource dataSource=new ComboPooledDataSource();
//设置驱动类:
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
//设置连接的URL:不理解
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai");
//设置连接数据库的用户名:
dataSource.setUser("root");
//设置密码:
dataSource.setPassword("root");
if(dataSource!=null){
System.out.println("连接到数据库!");
}
else{
System.out.println("连接失败!");
}
//设置连接池中初始连接数:
dataSource.setInitialPoolSize(3);
//设置最大连接数:
dataSource.setMaxPoolSize(5);
//设置最大空闲时间,超出最大空闲时间,连接断开:
dataSource.setMaxIdleTime(2000);
//从连接池获取链接对象:
Connection connection=dataSource.getConnection();
String sql="select *from user";
PreparedStatement statement =connection.prepareStatement(sql);
ResultSet resultSet=statement.executeQuery();
while(resultSet.next()){
String username=resultSet.getString("username");
System.out.println(username);
}
//关闭:把链接对象放回连接池;
//connection.close();
}
}
以上是引用xml前的: