thunderstorm 2020-06-06
public class c3p0Demo1 { public static void main(String[] args) throws SQLException { //1.创建数据库连接池对象 DataSource ds = new ComboPooledDataSource(); //使用默认配置 DataSource ds1 = new ComboPooledDataSource("otherc3p0"); //使用的是配置文件中第二种配置 最大连接数不一样 可以自己设置 //2.获取连接对象 Connection conn = ds.getConnection(); System.out.println(conn); } }
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/practise username=root password=woaini1314 #初始化连接数量 initialSize=5 # 最大连接数量 maxActive=10 #最大等待时间 maxWait=3000
import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * Druid连接池的工具类 */ public class JDBCUtils { //1.定义一个成员变量 DataSource private static DataSource ds; static { try { //1.加载配置文件 Properties pro = new Properties(); pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); //2.获取DataSource 并赋值 ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取连接 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } /** * 释放资源 */ public static void close(Statement stmt, Connection conn){ close(null,stmt,conn); //直接调用三个参数的方法 简化编译 } public static void close(ResultSet rs,Statement stmt, Connection conn){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close();//归还连接 } catch (SQLException e) { e.printStackTrace(); } } } /** * 获取连接池的方法 */ public static DataSource getDateSource(){ return ds; } }
工具类的定义
import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class druidDemo2 { public static void main(String[] args) { //完成添加操作 给account表添加一条记录 Connection conn = null; PreparedStatement pstmt = null; try { //1.获取连接 conn = JDBCUtils.getConnection(); //2.定义sql String sql = "insert into account values(null,?,?)"; //3.获取pstmt对象 pstmt = conn.prepareStatement(sql); //4.给问号赋值 pstmt.setString(1,"liudanao"); pstmt.setDouble(2,8000); //5.执行sql int count = pstmt.executeUpdate(); System.out.println(count); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtils.close(pstmt,conn); } } }
使用工具类添加数据
import cn.itcast.dataSource.utils.JDBCUtils; import org.junit.Test; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; public class JdbcTemplateDemo2 { //Junit单元测试,可以让方法独立执行 //1.获取JdbcTemplate对象 private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDateSource()); //1.修改1号数据的salary为10000 @Test public void test1(){ //2.定义sql String sql = "update emp set salary = 10000 where id = ?"; int count = template.update(sql, 1001); System.out.println(count); } //2.添加一条记录 @Test public void test2(){ String sql = "insert into emp(id,ename,dept_id) values(?,?,?)"; int count = template.update(sql, 1015, "郭靖", 10); System.out.println(count); } //3.删除刚才添加的记录 @Test public void test3(){ String sql = "delete from emp where id = ?"; int count = template.update(sql, 1015); System.out.println(count); } //4.查询id为1的记录,将其封装为map集合 @Test public void test4(){ String sql = "select * from emp where id = ?"; Map<String, Object> map = template.queryForMap(sql, 1001); //注意:这个方法查询的结果集长度只能是1 System.out.println(map); //{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20} } //5.查询所有记录,将其封装为list @Test public void test5(){ String sql = "select * from emp"; List<Map<String, Object>> list = template.queryForList(sql); for (Map<String, Object> result : list) { System.out.println(result); } } //6.查询所有记录,将其封装为Emp对象的list集合 @Test public void test6(){ String sql = "select * from emp"; List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class)); for (Emp emp : list) { System.out.println(emp); } } //7.查询总记录数 @Test public void test7(){ String sql = "select count(id) from emp"; Long total = template.queryForObject(sql, Long.class); System.out.println(total); } }
练习方法实现