1.JDBC连接池

  1. public class JdbcTemplateDemo2 {
  2.  
  3. //Junit单元测试,可以让方法独立执行
  4.  
  5. //1. 获取JDBCTemplate对象
  6. private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
  7. /**
  8. * 1. 修改1号数据的 salary 为 10000
  9. */
  10. @Test
  11. public void test1(){
  12.  
  13. //2. 定义sql
  14. String sql = "update emp set salary = 10000 where id = 1001";
  15. //3. 执行sql
  16. int count = template.update(sql);
  17. System.out.println(count);
  18. }
  19.  
  20. /**
  21. * 2. 添加一条记录
  22. */
  23. @Test
  24. public void test2(){
  25. String sql = "insert into emp(id,ename,dept_id) values(?,?,?)";
  26. int count = template.update(sql, 1015, "郭靖", 10);
  27. System.out.println(count);
  28.  
  29. }
  30.  
  31. /**
  32. * 3.删除刚才添加的记录
  33. */
  34. @Test
  35. public void test3(){
  36. String sql = "delete from emp where id = ?";
  37. int count = template.update(sql, 1015);
  38. System.out.println(count);
  39. }
  40.  
  41. /**
  42. * 4.查询id为1001的记录,将其封装为Map集合
  43. * 注意:这个方法查询的结果集长度只能是1
  44. */
  45. @Test
  46. public void test4(){
  47. String sql = "select * from emp where id = ? or id = ?";
  48. Map<String, Object> map = template.queryForMap(sql, 1001,1002);
  49. System.out.println(map);
  50. //{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}
  51.  
  52. }
  53.  
  54. /**
  55. * 5. 查询所有记录,将其封装为List
  56. */
  57. @Test
  58. public void test5(){
  59. String sql = "select * from emp";
  60. List<Map<String, Object>> list = template.queryForList(sql);
  61.  
  62. for (Map<String, Object> stringObjectMap : list) {
  63. System.out.println(stringObjectMap);
  64. }
  65. }
  66.  
  67. /**
  68. * 6. 查询所有记录,将其封装为Emp对象的List集合
  69. */
  70.  
  71. @Test
  72. public void test6(){
  73. String sql = "select * from emp";
  74. List<Emp> list = template.query(sql, new RowMapper<Emp>() {
  75.  
  76. @Override
  77. public Emp mapRow(ResultSet rs, int i) throws SQLException {
  78. Emp emp = new Emp();
  79. int id = rs.getInt("id");
  80. String ename = rs.getString("ename");
  81. int job_id = rs.getInt("job_id");
  82. int mgr = rs.getInt("mgr");
  83. Date joindate = rs.getDate("joindate");
  84. double salary = rs.getDouble("salary");
  85. double bonus = rs.getDouble("bonus");
  86. int dept_id = rs.getInt("dept_id");
  87.  
  88. emp.setId(id);
  89. emp.setEname(ename);
  90. emp.setJob_id(job_id);
  91. emp.setMgr(mgr);
  92. emp.setJoindate(joindate);
  93. emp.setSalary(salary);
  94. emp.setBonus(bonus);
  95. emp.setDept_id(dept_id);
  96.  
  97. return emp;
  98. }
  99. });
  100.  
  101. for (Emp emp : list) {
  102. System.out.println(emp);
  103. }
  104. }
  105.  
  106. /**
  107. * 6. 查询所有记录,将其封装为Emp对象的List集合
  108. */
  109.  
  110. @Test
  111. public void test6_2(){
  112. String sql = "select * from emp";
  113. List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
  114. for (Emp emp : list) {
  115. System.out.println(emp);
  116. }
  117. }
  118.  
  119. /**
  120. * 7. 查询总记录数
  121. */
  122.  
  123. @Test
  124. public void test7(){
  125. String sql = "select count(id) from emp";
  126. Long total = template.queryForObject(sql, Long.class);
  127. System.out.println(total);
  128. }
  129.  
  130. }
  1. /**
  2. * Druid连接池的工具类
  3. */
  4. public class JDBCUtils {
  5.  
  6. //1.定义成员变量 DataSource
  7. private static DataSource ds ;
  8.  
  9. static{
  10. try {
  11. //1.加载配置文件
  12. Properties pro = new Properties();
  13. pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
  14. //2.获取DataSource
  15. ds = DruidDataSourceFactory.createDataSource(pro);
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. /**
  24. * 获取连接
  25. */
  26. public static Connection getConnection() throws SQLException {
  27. return ds.getConnection();
  28. }
  29.  
  30. /**
  31. * 释放资源
  32. */
  33. public static void close(Statement stmt,Connection conn){
  34. /* if(stmt != null){
  35. try {
  36. stmt.close();
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. if(conn != null){
  43. try {
  44. conn.close();//归还连接
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }*/
  49.  
  50. close(null,stmt,conn);
  51. }
  52.  
  53. public static void close(ResultSet rs , Statement stmt, Connection conn){
  54.  
  55. if(rs != null){
  56. try {
  57. rs.close();
  58. } catch (SQLException e) {
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. if(stmt != null){
  64. try {
  65. stmt.close();
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. if(conn != null){
  72. try {
  73. conn.close();//归还连接
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79.  
  80. /**
  81. * 获取连接池方法
  82. */
  83.  
  84. public static DataSource getDataSource(){
  85. return ds;
  86. }
  87.  
  88. }
  1. driverClassName=com.mysql.jdbc.Driver
  2. url=jdbc:mysql:///db3
  3. username=root
  4. password=root
  5. # 初始化连接数量
  6. initialSize=5
  7. # 最大连接数
  8. maxActive=10
  9. # 最大等待时间
  10. maxWait=3000

2.JDBC druid连接池

  1. /**
  2. * 使用新的工具类
  3. */
  4. public class DruidDemo2 {
  5.  
  6. public static void main(String[] args) {
  7. /*
  8. * 完成添加操作:给account表添加一条记录
  9. */
  10. Connection conn = null;
  11. PreparedStatement pstmt = null;
  12. try {
  13. //1.获取连接
  14. conn = JDBCUtils.getConnection();
  15. //2.定义sql
  16. String sql = "insert into account values(null,?,?)";
  17. //3.获取pstmt对象
  18. pstmt = conn.prepareStatement(sql);
  19. //4.给?赋值
  20. pstmt.setString(1,"王五");
  21. pstmt.setDouble(2,3000);
  22. //5.执行sql
  23. int count = pstmt.executeUpdate();
  24. System.out.println(count);
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }finally {
  28. //6. 释放资源
  29. JDBCUtils.close(pstmt,conn);
  30. }
  31. }
  32.  
  33. }
  1. /**
  2. * Druid演示
  3. */
  4. public class DruidDemo {
  5. public static void main(String[] args) throws Exception {
  6. //1.导入jar包
  7. //2.定义配置文件
  8. //3.加载配置文件
  9. Properties pro = new Properties();
  10. InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
  11. pro.load(is);
  12. //4.获取连接池对象
  13. DataSource ds = DruidDataSourceFactory.createDataSource(pro);
  14. //5.获取连接
  15. Connection conn = ds.getConnection();
  16. System.out.println(conn);
  17.  
  18. }
  19. }

3.JDBC c3po连接池

  1. /**
  2. * c3p0的演示
  3. */
  4. public class C3P0Demo1 {
  5. public static void main(String[] args) throws SQLException {
  6. //1.创建数据库连接池对象
  7. DataSource ds = new ComboPooledDataSource();
  8. //2. 获取连接对象
  9. Connection conn = ds.getConnection();
  10.  
  11. //3. 打印
  12. System.out.println(conn);
  13.  
  14. }
  15. }
  1. /**
  2. * c3p0的演示
  3. */
  4. public class C3P0Demo1 {
  5. public static void main(String[] args) throws SQLException {
  6. //1.创建数据库连接池对象
  7. DataSource ds = new ComboPooledDataSource();
  8. //2. 获取连接对象
  9. Connection conn = ds.getConnection();
  10.  
  11. //3. 打印
  12. System.out.println(conn);
  13.  
  14. }
  15. }
  1. <c3p0-config>
  2. <!-- 使用默认的配置读取连接池对象 -->
  3. <default-config>
  4. <!-- 连接参数 -->
  5. <property name="driverClass">com.mysql.jdbc.Driver</property>
  6. <property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</property>
  7. <property name="user">root</property>
  8. <property name="password">root</property>
  9.  
  10. <!-- 连接池参数 -->
  11. <!--初始化申请的连接数量-->
  12. <property name="initialPoolSize">5</property>
  13. <!--最大的连接数量-->
  14. <property name="maxPoolSize">10</property>
  15. <!--超时时间-->
  16. <property name="checkoutTimeout">3000</property>
  17. </default-config>
  18.  
  19. <named-config name="otherc3p0">
  20. <!-- 连接参数 -->
  21. <property name="driverClass">com.mysql.jdbc.Driver</property>
  22. <property name="jdbcUrl">jdbc:mysql://localhost:3306/db3</property>
  23. <property name="user">root</property>
  24. <property name="password">root</property>
  25.  
  26. <!-- 连接池参数 -->
  27. <property name="initialPoolSize">5</property>
  28. <property name="maxPoolSize">8</property>
  29. <property name="checkoutTimeout">1000</property>
  30. </named-config>
  31. </c3p0-config>

JDBC2的更多相关文章

  1. [JDBC-2] JDBC CURD

    package com.amuos.jdbc.curd; import java.sql.Connection; import java.sql.ResultSet; import java.sql. ...

  2. JDBC2.0操作:结果集,更新,插入,删除,批处理语句

    JDBC对ResultSet的支持 JDBC最重要的概念是批处理,可以一次完成多个语句的执行. 可滚动的结果集. 如果想创建可滚动的结果集,则在创建PrepareStatement时候必须指定创建的类 ...

  3. JDBC2 --- 获取数据库连接的方式二 --- 技术搬运工(尚硅谷)

    /** * 方式二,对方式一的迭代 * 在如下的程序中,不出现第三方的api,使得程序具有更好的可移植性. * @throws Exception */ @Test public void testC ...

  4. 吴裕雄--天生自然JAVA数据库编程:JDBC2.0操作

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  5. JDBC-2(CRUD)

    3.PreparedStatement实现CRUD 3.1 操作和访问数据库 数据库连接被用于向数据库服务器发送命令和SQL语句,接受数据库服务器返回的结果.(一个数据库连接就是也给Socket连接) ...

  6. 浅谈Slick(2)- Slick101:第一个动手尝试的项目

    看完Slick官方网站上关于Slick3.1.1技术文档后决定开始动手建一个项目来尝试一下Slick功能的具体使用方法.我把这个过程中的一些了解和想法记录下来和大家一起分享.首先我用IntelliJ- ...

  7. spring 多数据源一致性事务方案

    spring 多数据源配置 spring 多数据源配置一般有两种方案: 1.在spring项目启动的时候直接配置两个不同的数据源,不同的sessionFactory.在dao 层根据不同业务自行选择使 ...

  8. JDBC 详解(转载)

    原文链接:http://blog.csdn.net/cai_xingyun/article/details/41482835 什么是JDBC? Java语言访问数据库的一种规范,是一套API JDBC ...

  9. 浅谈Slick(4)- Slick301:我的Slick开发项目设置

    前面几篇介绍里尝试了一些Slick的功能和使用方式,看来基本可以满足用scala语言进行数据库操作编程的要求,而且有些代码可以通过函数式编程模式来实现.我想,如果把Slick当作数据库操作编程主要方式 ...

随机推荐

  1. html上传图片后,在页面显示上传的图片

    html上传图片后,在页面显示上传的图片 1.html <form class="container" enctype="multipart/form-data&q ...

  2. leetcode-hard-array-11 Container With Most Water -NO

    mycode  time limited class Solution(object): def maxArea(self, height): """ :type hei ...

  3. js携带参数跳转controller返回页面

    upauth:function(){ var record = myForm.getRecord(); var companywyId = record.get("companyId&quo ...

  4. ConstraintLayout的简单介绍和使用

    ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能.我们都知道,在传统的Android开发当中,界面基本 ...

  5. 搭建Git服务器及本机克隆提交

    前文 Git是什么? Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首 ...

  6. 仿flash运动框架

    github地址: [https://github.com/linxd5/pictureShow] PS: 新建一个github项目很简单,只要new一个repo,后面按照提示做就可以了~ 项目思路: ...

  7. java:LeakFilling (Linux)

    1.Nosql 列数据库,没有update,非关系型数据库: 为了解决高并发.高可扩展.高可用.大数据存储问题而产生的数据库解决方案,就是NoSql数据库.  NoSQL,泛指非关系型的数据库,NoS ...

  8. lxml的XPath解析

    BeautifulSoup 可以将lxml作为默认的解析器使用,同样lxml可以单独使用.下面比较这两者之间优缺点: BeautifulSoup和lxml原理不一样,BeautifulSoup是基于D ...

  9. SolidWorks学习笔记4特征

    绘制斜的拉伸效果 一般拉伸方向垂直于草图基准面, 可以实现绘制一条线,作为其拉伸方向 效果如下 简单孔 在菜单中选择“插入”--“特征”---“简单直孔” 选择一个平面放置 设置好孔的直径和深度后,确 ...

  10. web端 微软 RDLC 报表插件 宽大于高 横向打印失效 解决方案

    起因于系统报表工具使用的RDLC,本地测试一直使用的纵向打印,未测试过横向打印