在我们程序员小白进行数据库连接的时候,普通的JDBC连接已经不能适用于项目的开发,所以我们就会使用到我们的数据库连接池,下面看秦哥胶片你怎么进行连接,首先我们必须导入我们的c3p0.jar和其它两个包,秦哥已经在下面给出了,小伙伴们可以去使用哦!
话不多说,看秦哥直接上代码:
首先是实体类
package qin.entity;
public class BookEntity {
private Integer id;
private String bookname;
private String state;
private int isdelete;
public BookEntity() {
}
public BookEntity(Integer id, String bookname, String state, Integer isdelete) {
this.id = id;
this.bookname = bookname;
this.state = state;
this.isdelete=isdelete;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getIsdelete() {
return isdelete;
}
public void setIsdelete(int isdelete) {
this.isdelete = isdelete;
}
@Override
public String toString() {
return "BookEntity{" +
"id=" + id +
", bookname='" + bookname + '\'' +
", state='" + state + '\'' +
", isdelete=" + isdelete +
'}';
}
}
public class Test01 {
public static void main(String[] args) throws PropertyVetoException, SQLException {
//1、创建c3p0的数据库连接池
ComboPooledDataSource pool = new ComboPooledDataSource();
//2、设置jdbc的连接信息
// pool.setUser("root");//设置我们的账户
// pool.setPassword("123");//设置我们的密码
// pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/mayikt?serverTimezone=UTC");//设置JdbcUrl
// pool.setDriverClass("com.mysql.jdbc.Driver");//设置我们的加载驱动
//底层会默认·读取src/c3p0-config.xml
//3、获取连接对象
Connection connection = pool.getConnection();
//4、获取预编译执行者对象 防止sql注入的问题
PreparedStatement preparedStatement= connection.prepareStatement("select * from book where id=?");
//5、设置我们的参数
preparedStatement.setInt(1,2);
//6、执行我们的sql语句
ResultSet resultSet = preparedStatement.executeQuery();
if(!resultSet.next())
{
return;
}
Integer dbid=resultSet.getInt("id");
String dbbookname=resultSet.getString("bookname");
String dbstate=resultSet.getString("state");
Integer dbisdelete=resultSet.getInt("isdelete");
BookEntity bookEntity = new BookEntity(dbid, dbbookname, dbstate,dbisdelete);
System.out.println(bookEntity);
resultSet.close();
connection.close();
}
}
然后就是配置文件c3p0-config.xml文件
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<!--需要修改自己数据库路径、用户账号、密码-->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/qin?serverTimezone=UTC</property>
<property name="user">root</property>
<property name="password">123</property>
<!--初始化申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--超时时间(单位毫秒)-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="mayikt-otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mayikt?serverTimezone=UTC</property>
<property name="user">root</property>
<property name="password">123</property>
</named-config>
</c3p0-config>
然后我们在看一下德鲁伊数据库连接池还是一样导入druid.jar包
然后秦哥直接上代码:
package hai.test;
import hai.entity.BookEntity;
import hai.utils.DataSourceUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test04 {
public static void main(String[] args) throws SQLException {
Connection connection=DataSourceUtils.getConnection();
//4、获取预编译执行者对象 防止sql注入的问题
PreparedStatement preparedStatement= connection.prepareStatement("select * from book where id=?");
//5、设置我们的参数
preparedStatement.setInt(1,2);
//6、执行我们的sql语句
ResultSet resultSet = preparedStatement.executeQuery();
if(!resultSet.next())
{
return;
}
Integer dbid=resultSet.getInt("id");
String dbbookname=resultSet.getString("bookname");
String dbstate=resultSet.getString("state");
Integer dbisdelete=resultSet.getInt("isdelete");
BookEntity bookEntity = new BookEntity(dbid, dbbookname, dbstate,dbisdelete);
System.out.println(bookEntity);
resultSet.close();
connection.close();
}
}
配置文件:druid.properties
# 加载数据库驱动
driverClassName=com.mysql.jdbc.Driver
# 连接数据库的url,db1表示数据库名,useSSL=false表示不使用SSL规范
url=jdbc:mysql://127.0.0.1:3306/mayikt?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
# 用户登录数据库的账号和密码
username=root
password=123
# 初始化连接数量
initialSize=5
# 最大连接数量
maxActive=10
# 最大等待时间
maxWait=3000
再加一个封装类:
package hai.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DataSourceUtils {
/**
* DataSource 数据库连接池对象
*/
private static DataSource dataSource;
/**
* 私有化构造方法
*/
private DataSourceUtils() {
}
/**
* 使用静态代码块加载配置文件
*/
static {
try {
// 读取配置文件
Properties properties = new Properties();
InputStream resourceAsStream = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(resourceAsStream);
// 获取数据库连接池
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 封装获取连接方法
*
* @return
* @throws SQLException
*/
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 封装关闭连接方法
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
这就是根据给广大网友的建议写出来的教程,希望能给你们一些帮助。