1 /** 2 The contents of this file are subject to the Mozilla Public License Version 1.1 3 (the "License"); you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 5 Software distributed under the License is distributed on an "AS IS" basis, 6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 7 specific language governing rights and limitations under the License. 8 9 The Original Code is "NormativeDatabase.java". Description: 10 "Point of access to a copy of the HL7 normative database" 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2001. All Rights Reserved. 14 15 Contributor(s): ______________________________________. 16 17 Alternatively, the contents of this file may be used under the terms of the 18 GNU General Public License (the �GPL�), in which case the provisions of the GPL are 19 applicable instead of those above. If you wish to allow use of your version of this 20 file only under the terms of the GPL and not to allow others to use your version 21 of this file under the MPL, indicate your decision by deleting the provisions above 22 and replace them with the notice and other provisions required by the GPL License. 23 If you do not delete the provisions above, a recipient may use your version of 24 this file under either the MPL or the GPL. 25 26 */ 27 28 package ca.uhn.hl7v2.database; 29 30 import java.sql.Connection; 31 import java.sql.DriverManager; 32 import java.sql.ResultSet; 33 import java.sql.SQLException; 34 import java.sql.Statement; 35 36 import org.slf4j.Logger; 37 import org.slf4j.LoggerFactory; 38 39 /** 40 * <p> 41 * Point of access to a copy of the HL7 normative database. A typical way of obtaining and using a 42 * database connection would be ... 43 * </p> 44 * <p> 45 * <code>Connection c = NormativeDatabase.getInstance().getConnection();<br> 46 * // ... use the database ... <br> 47 * NormativeDatabase.returnConnection(c);</code> 48 * </p> 49 * <p> 50 * Since the database may be installed differently on different systems, certain system properties 51 * must be set with the required connection information, as follows: 52 * </p> 53 * <p> 54 * <code>ca.on.uhn.hl7.database.user</code> - the user ID needed to connect (if required)<br> 55 * <code>ca.on.uhn.hl7.database.passsword</code> - the password associated with the above user (if 56 * required) 57 * </p> 58 * <p> 59 * The required JDBC driver must also be loaded (this can be done by ensuring that the required 60 * driver appears in the classpath and appending the class name to the "jdbc.drivers" system 61 * property. 62 * 63 * @author Bryan Tripp (bryan_tripp@sourceforge.net) 64 */ 65 public class NormativeDatabase 66 { 67 /** User system property constant */ 68 public static final String PROP_DATABASE_USER = "ca.on.uhn.hl7.database.user"; 69 /** User system property constant */ 70 public static final String PROP_DATABASE_URL = "ca.on.uhn.hl7.database.url"; 71 /** User system property constant */ 72 public static final String PROP_DATABASE_PASSWORD = "ca.on.uhn.hl7.database.password"; 73 74 private static final Logger log = LoggerFactory.getLogger(NormativeDatabase.class); 75 76 private static final NormativeDatabasetml#NormativeDatabase">NormativeDatabase INSTANCE = new NormativeDatabase(); 77 78 private String dbUrl; 79 private String dbUser; 80 private String dbPassword; 81 82 private Connection connection; 83 private SQLException checkedOut; 84 85 86 /** 87 * Private constructor 88 */ 89 private NormativeDatabase() { 90 // nothing 91 } 92 93 94 /** 95 * Returns the singleton instance of NormativeDatabase. 96 */ 97 public static NormativeDatabase getInstance() { 98 return INSTANCE; 99 } 100 101 102 /** 103 * Provides a Connection to the normative database. A new connection may be created if none are 104 * available. 105 */ 106 public synchronized Connection getConnection() throws SQLException { 107 if (checkedOut != null) { 108 throw checkedOut; 109 } 110 111 String user = System.getProperty(PROP_DATABASE_USER); 112 String pass = System.getProperty(PROP_DATABASE_PASSWORD); 113 String url = System.getProperty(PROP_DATABASE_URL); 114 115 if (!url.equals(dbUrl) || !pass.equals(dbPassword) || !user.equals(dbUser)) { 116 if (connection != null) { 117 connection.close(); 118 } 119 connection = DriverManager.getConnection(url, user, pass); 120 dbUrl = url; 121 dbPassword = pass; 122 dbUser = user; 123 } 124 125 checkedOut = new SQLException("COnnection already checked out"); 126 return connection; 127 } 128 129 130 /** 131 * Used to return an HL7 normative database connection to the connection pool. If the given 132 * connection is not in fact a connection to the normative database, it is discarded. 133 */ 134 public void returnConnection(Connection conn) { 135 checkedOut = null; 136 } 137 138 139 // test 140 public static void main(String[] args) { 141 try { 142 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 143 Connection conn = NormativeDatabase.getInstance().getConnection(); 144 Statement stmt = conn.createStatement(); 145 ResultSet rs = stmt.executeQuery("select * from TableValues"); 146 while (rs.next()) { 147 Object tabNum = rs.getObject(1); 148 Object val = rs.getObject(3); 149 Object desc = rs.getObject(4); 150 System.out.println("Table: " + tabNum + " Value: " + val + " Description: " + desc); 151 } 152 } catch (SQLException | ClassNotFoundException e) { 153 log.error("test msg!!", e); 154 } 155 } 156 157 }