1 6 7 package com.quikj.server.web; 8 9 import java.sql.*; 10 11 15 public class JDBCConnection 16 { 17 18 private String className = null; 19 20 private String url = null; 21 22 private String host = null; 23 24 private String user = null; 25 26 private String password = null; 27 28 private static JDBCConnection instance = null; 29 30 private static String errorMessage = ""; 31 32 33 public JDBCConnection(String class_name, 34 String url, 35 String host, 36 String user, 37 String password) 38 { 39 setClassName(class_name); 40 setUrl(url); 41 setHost(host); 42 setUser(user); 43 setPassword(password); 44 45 instance = this; 46 } 47 48 public static JDBCConnection getInstance() 49 { 50 return instance; 51 } 52 53 56 public java.lang.String getClassName() 57 { 58 return className; 59 } 60 61 64 public void setClassName(java.lang.String className) 65 { 66 this.className = className; 67 } 68 69 72 public java.lang.String getHost() 73 { 74 return host; 75 } 76 77 80 public void setHost(java.lang.String host) 81 { 82 this.host = host; 83 } 84 85 88 public java.lang.String getPassword() 89 { 90 return password; 91 } 92 93 96 public void setPassword(java.lang.String password) 97 { 98 this.password = password; 99 } 100 101 104 public java.lang.String getUrl() 105 { 106 return url; 107 } 108 109 112 public void setUrl(java.lang.String url) 113 { 114 this.url = url; 115 } 116 117 120 public java.lang.String getUser() 121 { 122 return user; 123 } 124 125 128 public void setUser(java.lang.String user) 129 { 130 this.user = user; 131 } 132 133 public Connection getNewConnection(String database) 134 { 135 Connection connection = null; 136 try 138 { 139 Class.forName(className).newInstance(); 140 141 if (host.equals("localhost") == true) 142 { 143 host = "127.0.0.1"; 144 } 145 146 String url_str = url + "://" + host + "/" + database; 147 connection = DriverManager.getConnection(url_str, user, password); 148 149 if (connection == null) 150 { 151 errorMessage = 152 "Connection to the database server " 153 + host + " database " + database + " (" + user + ") failed"; 154 155 return null; 156 } 157 158 } 159 catch (SQLException ex) 160 { 161 errorMessage = 163 "An SQL error occured while trying to connect to the database server - " 164 + ex.getMessage(); 165 166 return null; 167 } 168 catch (Exception ex) 169 { 170 errorMessage = 172 "An error (" 173 + ex.getClass().getName() 174 + ") occured while trying to connect to the database server - " 175 + ex.getMessage(); 176 return null; 177 } 178 179 return connection; 180 } 181 182 185 public static java.lang.String getErrorMessage() 186 { 187 return errorMessage; 188 } 189 190 } 191 | Popular Tags |