Guest User

Untitled

a guest
Jul 8th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. package ro.teamnet.zth.api.database;
  2.  
  3. import static ro.teamnet.zth.api.database.DBProperties.*;
  4. import java.sql.*;
  5.  
  6. /**
  7. * Created by user on 7/8/2016.
  8. */
  9. public class DBManager implements DBProperties{
  10.  
  11.  
  12. private static boolean registerDriverStatus = false;
  13. private static final String CONNECTION_STRING = "jdbc:oracle:thin:@" + DBProperties.IP + ":" + DBProperties.PORT + ";";
  14.  
  15.  
  16. public DBManager()throws UnsupportedOperationException{
  17. }
  18.  
  19. private static boolean registerDriver() throws ClassNotFoundException {
  20.  
  21. if(registerDriverStatus == false) {
  22. Class.forName("oracle.jdbc.driver.OracleDriver");
  23. registerDriverStatus = true;
  24. return registerDriverStatus;
  25. }
  26. return registerDriverStatus;
  27. }
  28.  
  29. private static Connection getConnection() throws ClassNotFoundException, SQLException {
  30. Connection conn = null;
  31. registerDriver();
  32. try {
  33. conn = DriverManager.getConnection(CONNECTION_STRING, USER, PASS);
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. return conn;
  39. }
  40.  
  41. public static boolean checkConnection(Connection connection) throws SQLException, ClassNotFoundException {
  42.  
  43. Connection conn = getConnection();
  44. try (Statement stmt = conn.createStatement( )){
  45. stmt.execute("SELECT 1 FROM DUAL");
  46. }
  47. catch (SQLException e) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. }
Add Comment
Please, Sign In to add comment