Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. public interface ConnectionDB {
  2. public Connection getConnection();
  3. }
  4.  
  5. public class GenericConnection {
  6.  
  7. protected static Properties dbProperties = new Properties();
  8.  
  9. public static Properties getDbProperties() {
  10. if(dbProperties == null){
  11. try {
  12. dbProperties.load(new FileInputStream("src/properties/conf.properties"));
  13. } catch (FileNotFoundException ex) {
  14. Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
  15. } catch (IOException ex) {
  16. Logger.getLogger(GenericConnection.class.getName()).log(Level.SEVERE, null, ex);
  17. }
  18. }
  19. return dbProperties;
  20. }
  21.  
  22. public static void setDbProperties(Properties dbProperties) {
  23. GenericConnection.dbProperties = dbProperties;
  24. }
  25. }
  26.  
  27. public class OracleConnection extends GenericConnection implements ConnectionDB {
  28.  
  29. private Connection conn;
  30. private Statement st;
  31.  
  32. public Statement getSt() {
  33. return st;
  34. }
  35.  
  36. public void setSt(Statement st) {
  37. this.st = st;
  38. }
  39.  
  40. public Connection getConn() {
  41. return conn;
  42. }
  43.  
  44. public void setConn(Connection conn) {
  45. this.conn = conn;
  46. }
  47.  
  48. @Override
  49. public Connection getConnection() {
  50. if (getConn() != null) {
  51. return getConn();
  52. } else {
  53. try {
  54. String url = "jdbc:oracle:thin:@" + getDbProperties().getProperty("ServerOracle") + ":" + getDbProperties().getProperty("portOracle") + ":" + getDbProperties().getProperty("sidOracle");
  55. setConn(DriverManager.getConnection(url, getDbProperties().getProperty("userOracle"), getDbProperties().getProperty("passwdOracle")));
  56. setSt(getConn().createStatement());
  57. return getConn();
  58. } catch (SQLException ex) {
  59. Logger.getLogger(OracleConnection.class.getName()).log(Level.SEVERE, null, ex);
  60. return null;
  61. }
  62. }
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement