微信登录

JDBC - 事务多sql - Xxxc.java - try{}catch{}

文件名

XxxControl.java
不用test,或者你自己可以看
try{}catch{}finally{}

示例

  1. try{
  2. JDBCUtils.getConnection //连接
  3. Xxx xxx = new Xxx(数据);
  4. dao.insert(conn, xxx);//操作
  5. }
  6. catch(Exception e) { e.printStackTrace();//异常
  7. }
  8. finally{
  9. JDBCUtils.closeResource(conn, null);//关闭连接
  10. }
  1. package com.atguigu3.dao.junit;
  2. import static org.junit.Assert.*;
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.util.List;
  6. import org.junit.Test;
  7. import com.atguigu4.util.JDBCUtils;
  8. import com.atguigu2.bean.Customer;
  9. import com.atguigu3.dao.CustomerDAOImpl;
  10. public class CustomerDAOImplTest {
  11. private CustomerDAOImpl dao = new CustomerDAOImpl();
  12. @Test
  13. public void testInsert() {
  14. Connection conn = null;
  15. try {
  16. conn = JDBCUtils.getConnection();
  17. Customer cust = new Customer(1, "于小飞", "xiaofei@126.com",new Date(43534646435L));
  18. dao.insert(conn, cust);
  19. System.out.println("添加成功");
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }finally{
  23. JDBCUtils.closeResource(conn, null);
  24. }
  25. }
  26. @Test
  27. public void testDeleteById() {
  28. Connection conn = null;
  29. try {
  30. conn = JDBCUtils.getConnection();
  31. dao.deleteById(conn, 13);
  32. System.out.println("删除成功");
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }finally{
  36. JDBCUtils.closeResource(conn, null);
  37. }
  38. }
  39. @Test
  40. public void testUpdateConnectionCustomer() {
  41. Connection conn = null;
  42. try {
  43. conn = JDBCUtils.getConnection();
  44. Customer cust = new Customer(18,"贝多芬","beiduofen@126.com",new Date(453465656L));
  45. dao.update(conn, cust);
  46. System.out.println("修改成功");
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }finally{
  50. JDBCUtils.closeResource(conn, null);
  51. }
  52. }
  53. @Test
  54. public void testGetCustomerById() {
  55. Connection conn = null;
  56. try {
  57. conn = JDBCUtils.getConnection3();
  58. Customer cust = dao.getCustomerById(conn, 19);
  59. System.out.println(cust);
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }finally{
  63. JDBCUtils.closeResource(conn, null);
  64. }
  65. }
  66. @Test
  67. public void testGetAll() {
  68. Connection conn = null;
  69. try {
  70. conn = JDBCUtils.getConnection();
  71. List<Customer> list = dao.getAll(conn);
  72. list.forEach(System.out::println);
  73. System.out.println("");
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }finally{
  77. JDBCUtils.closeResource(conn, null);
  78. }
  79. }
  80. @Test
  81. public void testGetCount() {
  82. Connection conn = null;
  83. try {
  84. conn = JDBCUtils.getConnection();
  85. Long count = dao.getCount(conn);
  86. System.out.println("表中的记录数为:" + count);
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }finally{
  90. JDBCUtils.closeResource(conn, null);
  91. }
  92. }
  93. @Test
  94. public void testGetMaxBirth() {
  95. Connection conn = null;
  96. try {
  97. conn = JDBCUtils.getConnection();
  98. Date maxBirth = dao.getMaxBirth(conn);
  99. System.out.println("最大的生日为:" + maxBirth);
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }finally{
  103. JDBCUtils.closeResource(conn, null);
  104. }
  105. }
  106. }
JDBC - 事务多sql - Xxxc.java - try{}catch{}