Guest User

Untitled

a guest
Dec 13th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.mycompany.mavenproject1;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13.  
  14. /**
  15. *
  16. * @author surasit
  17. */
  18. public class DbHelper {
  19. Connection conn = null;
  20. String tableName = "";
  21. public DbHelper(String tableName) {
  22. this.tableName = tableName;
  23. try {
  24. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name?" +
  25. "user=yourusername&password=yourpassword ");
  26. } catch (SQLException ex) {
  27. // handle any errors
  28. System.out.println("SQLException: " + ex.getMessage());
  29. System.out.println("SQLState: " + ex.getSQLState());
  30. System.out.println("VendorError: " + ex.getErrorCode());
  31. }
  32. }
  33.  
  34. public void exeQuery(String command) {
  35. System.out.println("[COMMAND]:" + command);
  36. Statement stmt = null;
  37. ResultSet rs = null;
  38. try {
  39. stmt = this.conn.createStatement();
  40. rs = stmt.executeQuery(command);
  41. rs.next();
  42. }
  43. catch (SQLException ex){
  44. // handle any errors
  45. System.out.println("SQLException: " + ex.getMessage());
  46. System.out.println("SQLState: " + ex.getSQLState());
  47. System.out.println("VendorError: " + ex.getErrorCode());
  48. }
  49. finally {
  50. // it is a good idea to release
  51. // resources in a finally{} block
  52. // in reverse-order of their creation
  53. // if they are no-longer needed
  54.  
  55. if (rs != null) {
  56. try {
  57. rs.close();
  58. } catch (SQLException sqlEx) { } // ignore
  59.  
  60. rs = null;
  61. }
  62.  
  63. if (stmt != null) {
  64. try {
  65. stmt.close();
  66. } catch (SQLException sqlEx) { } // ignore
  67.  
  68. stmt = null;
  69. }
  70. }
  71. }
  72.  
  73. public void execUpdate(String command) {
  74. System.out.println("[COMMAND]:" + command);
  75. Statement stmt = null;
  76. ResultSet rs = null;
  77. try {
  78. stmt = this.conn.createStatement();
  79. stmt.executeUpdate(command);
  80. }
  81. catch (SQLException ex){
  82. // handle any errors
  83. System.out.println("SQLException: " + ex.getMessage());
  84. System.out.println("SQLState: " + ex.getSQLState());
  85. System.out.println("VendorError: " + ex.getErrorCode());
  86. }
  87. finally {
  88. // it is a good idea to release
  89. // resources in a finally{} block
  90. // in reverse-order of their creation
  91. // if they are no-longer needed
  92.  
  93. if (rs != null) {
  94. try {
  95. rs.close();
  96. } catch (SQLException sqlEx) { } // ignore
  97.  
  98. rs = null;
  99. }
  100.  
  101. if (stmt != null) {
  102. try {
  103. stmt.close();
  104. } catch (SQLException sqlEx) { } // ignore
  105.  
  106. stmt = null;
  107. }
  108. }
  109. }
  110.  
  111. public void clearAll() {
  112. this.execUpdate("TRUNCATE " + this.tableName);
  113. }
  114. }
  115.  
  116. // usage
  117. public class TestBalance {
  118.  
  119. public TestBalance() {
  120. balanceHelper = new DbHelper("table_name");
  121. }
  122.  
  123.  
  124. @Before
  125. public void setUp() {
  126. balanceHelper.execUpdate("sql statement");
  127. }
  128.  
  129. @After
  130. public void tearDown() {
  131. balanceHelper.clearAll();
  132. }
  133.  
  134. // your test here.
  135. }
Add Comment
Please, Sign In to add comment