Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. package com.mysql.jdbc.jdbc2.optional;
  2.  
  3. import com.mysql.jdbc.ConnectionPropertiesImpl;
  4. import com.mysql.jdbc.NonRegisteringDriver;
  5. import java.io.PrintWriter;
  6. import java.io.Serializable;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.util.Iterator;
  10. import java.util.Properties;
  11. import java.util.Set;
  12. import javax.naming.NamingException;
  13. import javax.naming.Reference;
  14. import javax.naming.Referenceable;
  15. import javax.naming.StringRefAddr;
  16. import javax.sql.DataSource;
  17.  
  18. public class MysqlDataSource
  19. extends ConnectionPropertiesImpl
  20. implements DataSource, Referenceable, Serializable
  21. {
  22. protected static NonRegisteringDriver mysqlDriver = null;
  23.  
  24. static
  25. {
  26. try
  27. {
  28. mysqlDriver = new NonRegisteringDriver();
  29. }
  30. catch (Exception E)
  31. {
  32. throw new RuntimeException("Can not load Driver class com.mysql.jdbc.Driver");
  33. }
  34. }
  35.  
  36. protected PrintWriter logWriter = null;
  37. protected String databaseName = null;
  38. protected String encoding = null;
  39. protected String hostName = null;
  40. protected String password = null;
  41. protected String profileSql = "false";
  42. protected String url = null;
  43. protected String user = null;
  44. protected boolean explicitUrl = false;
  45. protected int port = 3306;
  46.  
  47. public Connection getConnection()
  48. throws SQLException
  49. {
  50. return getConnection(this.user, this.password);
  51. }
  52.  
  53. public Connection getConnection(String userID, String pass)
  54. throws SQLException
  55. {
  56. Properties props = new Properties();
  57. if (userID != null) {
  58. props.setProperty("user", userID);
  59. }
  60. if (pass != null) {
  61. props.setProperty("password", pass);
  62. }
  63. exposeAsProperties(props);
  64.  
  65. return getConnection(props);
  66. }
  67.  
  68. public void setDatabaseName(String dbName)
  69. {
  70. this.databaseName = dbName;
  71. }
  72.  
  73. public String getDatabaseName()
  74. {
  75. return this.databaseName != null ? this.databaseName : "";
  76. }
  77.  
  78. public void setLogWriter(PrintWriter output)
  79. throws SQLException
  80. {
  81. this.logWriter = output;
  82. }
  83.  
  84. public PrintWriter getLogWriter()
  85. {
  86. return this.logWriter;
  87. }
  88.  
  89. public int getLoginTimeout()
  90. {
  91. return 0;
  92. }
  93.  
  94. public void setPassword(String pass)
  95. {
  96. this.password = pass;
  97. }
  98.  
  99. public void setPort(int p)
  100. {
  101. this.port = p;
  102. }
  103.  
  104. public int getPort()
  105. {
  106. return this.port;
  107. }
  108.  
  109. public void setPortNumber(int p)
  110. {
  111. setPort(p);
  112. }
  113.  
  114. public int getPortNumber()
  115. {
  116. return getPort();
  117. }
  118.  
  119. public void setPropertiesViaRef(Reference ref)
  120. throws SQLException
  121. {
  122. super.initializeFromRef(ref);
  123. }
  124.  
  125. public Reference getReference()
  126. throws NamingException
  127. {
  128. String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
  129. Reference ref = new Reference(getClass().getName(), factoryName, null);
  130. ref.add(new StringRefAddr("user", getUser()));
  131.  
  132. ref.add(new StringRefAddr("password", this.password));
  133.  
  134. ref.add(new StringRefAddr("serverName", getServerName()));
  135. ref.add(new StringRefAddr("port", "" + getPort()));
  136. ref.add(new StringRefAddr("databaseName", getDatabaseName()));
  137. ref.add(new StringRefAddr("url", getUrl()));
  138. ref.add(new StringRefAddr("explicitUrl", String.valueOf(this.explicitUrl)));
  139. try
  140. {
  141. storeToRef(ref);
  142. }
  143. catch (SQLException sqlEx)
  144. {
  145. throw new NamingException(sqlEx.getMessage());
  146. }
  147. return ref;
  148. }
  149.  
  150. public void setServerName(String serverName)
  151. {
  152. this.hostName = serverName;
  153. }
  154.  
  155. public String getServerName()
  156. {
  157. return this.hostName != null ? this.hostName : "";
  158. }
  159.  
  160. public void setURL(String url)
  161. {
  162. setUrl(url);
  163. }
  164.  
  165. public String getURL()
  166. {
  167. return getUrl();
  168. }
  169.  
  170. public void setUrl(String url)
  171. {
  172. this.url = url;
  173. this.explicitUrl = true;
  174. }
  175.  
  176. public String getUrl()
  177. {
  178. if (!this.explicitUrl)
  179. {
  180. String builtUrl = "jdbc:mysql://";
  181. builtUrl = builtUrl + getServerName() + ":" + getPort() + "/" + getDatabaseName();
  182.  
  183. return builtUrl;
  184. }
  185. return this.url;
  186. }
  187.  
  188. public void setUser(String userID)
  189. {
  190. this.user = userID;
  191. }
  192.  
  193. public String getUser()
  194. {
  195. return this.user;
  196. }
  197.  
  198. protected Connection getConnection(Properties props)
  199. throws SQLException
  200. {
  201. String jdbcUrlToUse = null;
  202. if (!this.explicitUrl)
  203. {
  204. StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");
  205. if (this.hostName != null) {
  206. jdbcUrl.append(this.hostName);
  207. }
  208. jdbcUrl.append(":");
  209. jdbcUrl.append(this.port);
  210. jdbcUrl.append("/");
  211. if (this.databaseName != null) {
  212. jdbcUrl.append(this.databaseName);
  213. }
  214. jdbcUrlToUse = jdbcUrl.toString();
  215. }
  216. else
  217. {
  218. jdbcUrlToUse = this.url;
  219. }
  220. Properties urlProps = mysqlDriver.parseURL(jdbcUrlToUse, null);
  221. urlProps.remove("DBNAME");
  222. urlProps.remove("HOST");
  223. urlProps.remove("PORT");
  224.  
  225. Iterator keys = urlProps.keySet().iterator();
  226. while (keys.hasNext())
  227. {
  228. String key = (String)keys.next();
  229.  
  230. props.setProperty(key, urlProps.getProperty(key));
  231. }
  232. return mysqlDriver.connect(jdbcUrlToUse, props);
  233. }
  234.  
  235. public void setLoginTimeout(int seconds)
  236. throws SQLException
  237. {}
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement