1 6 7 package com.quikj.application.communicator.admin.model; 8 9 import java.sql.*; 10 11 15 public class DBConnection 16 { 17 private static DBConnection instance = null; 18 19 20 private String jdbcDriver; 21 22 23 private String url; 24 25 26 private String user; 27 28 29 private String password; 30 31 32 private String errorMessage = ""; 33 34 35 36 public DBConnection() 37 { 38 instance = this; 39 } 40 41 public static DBConnection getInstance() 42 { 43 return instance; 44 } 45 46 50 public String getJdbcDriver() 51 { 52 return this.jdbcDriver; 53 } 54 55 59 public void setJdbcDriver(String jdbcDriver) 60 { 61 this.jdbcDriver = jdbcDriver; 62 } 63 64 68 public String getUrl() 69 { 70 return this.url; 71 } 72 73 77 public void setUrl(String url) 78 { 79 this.url = url; 80 } 81 82 86 public String getUser() 87 { 88 return this.user; 89 } 90 91 95 public void setUser(String user) 96 { 97 this.user = user; 98 } 99 100 104 public String getPassword() 105 { 106 return this.password; 107 } 108 109 113 public void setPassword(String password) 114 { 115 this.password = password; 116 } 117 118 122 public String getErrorMessage() 123 { 124 return this.errorMessage; 125 } 126 127 public Connection getConnection() 128 { 129 Connection connection = null; 130 try 131 { 132 Class.forName(jdbcDriver).newInstance(); 133 134 connection = DriverManager.getConnection(url, user, password); 135 136 if (connection == null) 137 { 138 errorMessage = "Connection to the database url " 139 + url + " (as " + user + ") failed"; 140 141 return null; 142 } 143 } 144 catch (Exception ex) 145 { 146 errorMessage = ex.getClass().getName() + ": " + ex.getMessage() + ". URL = " + url; 147 return null; 148 } 149 150 return connection; 151 } 152 153 } 154 | Popular Tags |