Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.mavenproject1;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- /**
- *
- * @author surasit
- */
- public class DbHelper {
- Connection conn = null;
- String tableName = "";
- public DbHelper(String tableName) {
- this.tableName = tableName;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name?" +
- "user=yourusername&password=yourpassword ");
- } catch (SQLException ex) {
- // handle any errors
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- }
- public void exeQuery(String command) {
- System.out.println("[COMMAND]:" + command);
- Statement stmt = null;
- ResultSet rs = null;
- try {
- stmt = this.conn.createStatement();
- rs = stmt.executeQuery(command);
- rs.next();
- }
- catch (SQLException ex){
- // handle any errors
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- finally {
- // it is a good idea to release
- // resources in a finally{} block
- // in reverse-order of their creation
- // if they are no-longer needed
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) { } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) { } // ignore
- stmt = null;
- }
- }
- }
- public void execUpdate(String command) {
- System.out.println("[COMMAND]:" + command);
- Statement stmt = null;
- ResultSet rs = null;
- try {
- stmt = this.conn.createStatement();
- stmt.executeUpdate(command);
- }
- catch (SQLException ex){
- // handle any errors
- System.out.println("SQLException: " + ex.getMessage());
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("VendorError: " + ex.getErrorCode());
- }
- finally {
- // it is a good idea to release
- // resources in a finally{} block
- // in reverse-order of their creation
- // if they are no-longer needed
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) { } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) { } // ignore
- stmt = null;
- }
- }
- }
- public void clearAll() {
- this.execUpdate("TRUNCATE " + this.tableName);
- }
- }
- // usage
- public class TestBalance {
- public TestBalance() {
- balanceHelper = new DbHelper("table_name");
- }
- @Before
- public void setUp() {
- balanceHelper.execUpdate("sql statement");
- }
- @After
- public void tearDown() {
- balanceHelper.clearAll();
- }
- // your test here.
- }
Add Comment
Please, Sign In to add comment