C3P0连接池操作方法
使用DBUtils实现查询操作
使用DBUtils实现增删改操作
通过配置文件方式步骤:
- 导入DRUID jar 包
- 拷贝配置文件到src目录
- 根据配置文件创建Druid连接池对象
- 从Druid连接池对象获得Connection
配置文件
#数据库连接参数url=jdbc:mysql://localhost:3306/day21_1username=rootpassword=rootdriverClassName=com.mysql.jdbc.Driver# 连接池的参数initialSize=5maxActive=15maxWait=2000
编写Java代码
import com.itheima.C3po.User;import org.junit.Test;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class Peizhi {@Test //查询一条数据public void select() throws Exception {//拷贝jar包,添加classpath路径/* //创建Druid连接池对象Properties prop=new Properties();//加载配置文件InputStream is=Peizhi.class.getClassLoader().getResourceAsStream(\"druid.properties\");prop.load(is);//创建Druid连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);//获得连接Connection connection = dataSource.getConnection();*/Connection connection = DruidUtils.getConnection();//书写sql语句,预编译sql语句,得到预编译对象String sql=\"select * from user where id=?\";PreparedStatement ps = connection.prepareStatement(sql);//设置参数ps.setInt(1,2);//执行sql语句,处理数据ResultSet resultSet = ps.executeQuery();//封装,处理数据User user=null;while (resultSet.next()) {user=new User();user.setId(resultSet.getInt(\"id\"));user.setUsername(resultSet.getString(\"username\"));user.setPassword(resultSet.getString(\"password\"));user.setNickname(resultSet.getString(\"nickname\"));}System.out.println(user);//释放资源DruidUtils.release(resultSet,ps,connection);}@Test //修改一条数据public void update() throws SQLException {//获取连接Connection connection = DruidUtils.getConnection();//书写sql语句,预编译sql语句,得到预编译对象String sql=\"update user set password=? where id=?\";PreparedStatement ps = connection.prepareStatement(sql);//设置参数ps.setString(1,\"66666\");ps.setInt(2,3);//执行sql语句,处理数据int i = ps.executeUpdate();System.out.println(\"受影响的行数:\"+i);//释放资源,连接归还DruidUtils.release(null,ps,connection);}}
抽取工具类DruidUtils
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 DruidUtils {private static DataSource dataSource=null;static {//创建Druid连接池对象try {Properties prop=new Properties();//加载配置文件InputStream is=Peizhi.class.getClassLoader().getResourceAsStream(\"druid.properties\");prop.load(is);//创建Druid连接池对象dataSource = DruidDataSourceFactory.createDataSource(prop);} catch (Exception e) {e.printStackTrace();} finally {}}//定义getConnection()方法从DateSource获得连接public static Connection getConnection () throws SQLException {//获得连接Connection connection = dataSource.getConnection();return connection;}//定义release方法,释放资源public static void release(ResultSet resultSet, Statement statement,Connection connection){if (resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (statement!=null){try {statement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}}}