001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.examples.db;
020:
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.PrintWriter;
024: import java.sql.DriverManager;
025: import java.sql.SQLException;
026: import java.util.Properties;
027:
028: /**
029: * The example uses a <code>SimpleDataSource</code> to connect to the database.
030: * Works with the settings loaded from the file <strong>exampledb.properties</strong>.
031: * In particular, the driver class name and the database url are configured in this file.
032: * The JDBC driver library needs to be in the classpath.
033: * @since 2.2
034: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
035: * @version 3.4 <7 March 05>
036: */
037: public class SimpleDataSource implements javax.sql.DataSource,
038: java.io.Serializable {
039: private String url = null;
040: private String driver = null;
041: private String user = null;
042: private String password = null;
043:
044: /**
045: * Constructor.
046: */
047: public SimpleDataSource() {
048: super ();
049: Properties dbProperties = new Properties();
050: try {
051: dbProperties.load(new FileInputStream(
052: "exampledb.properties"));
053: url = dbProperties.getProperty("url");
054: driver = dbProperties.getProperty("driver");
055: user = dbProperties.getProperty("user");
056: password = dbProperties.getProperty("password");
057: } catch (IOException x) {
058: System.err
059: .println("Cannot load file with database settings: exampledb.properties");
060: }
061: }
062:
063: /**
064: * Get a database connection.
065: * @return a database connection
066: */
067: public java.sql.Connection getConnection()
068: throws java.sql.SQLException {
069: return getConnection(user, password);
070: }
071:
072: /**
073: * Get a database connection.
074: * @return a database connection
075: * @param userName a user name
076: * @param password a password
077: */
078: public java.sql.Connection getConnection(String userName,
079: String password) throws java.sql.SQLException {
080: // try to load driver
081: try {
082: Class.forName(driver);
083: } catch (Exception x) {
084: System.err.println("Driver class not found: " + driver);
085: }
086: // get connection
087: return DriverManager.getConnection(url, userName, password);
088: }
089:
090: /**
091: * Get the login timeout.
092: */
093: public int getLoginTimeout() throws SQLException {
094: return 1000;
095: }
096:
097: /**
098: * Get a log writer.
099: * @return a log writer
100: */
101: public PrintWriter getLogWriter() throws SQLException {
102: return null;
103: }
104:
105: /**
106: * Set the login timeout.
107: * @param millis the time in milli seconds
108: */
109: public void setLoginTimeout(int millis) throws SQLException {
110: }
111:
112: /**
113: * Set the log writer.
114: * @param writer a writer
115: */
116: public void setLogWriter(PrintWriter writer) throws SQLException {
117: }
118:
119: /**
120: * Getter for driver property.
121: * @return the driver class name
122: */
123: public String getDriver() {
124: return driver;
125: }
126:
127: /**
128: * Setter for the driver property.
129: * @param value a driver class name
130: */
131: public void setDriver(String value) {
132: driver = value;
133: }
134:
135: /**
136: * Getter for user property.
137: * @return the user
138: */
139: public String getUser() {
140: return user;
141: }
142:
143: /**
144: * Setter for the user property.
145: * @param value the user
146: */
147: public void setUser(String value) {
148: user = value;
149: }
150:
151: /**
152: * Getter for password property.
153: * @return the password
154: */
155: public String getPassword() {
156: return password;
157: }
158:
159: /**
160: * Setter for the password property.
161: * @param value the password
162: */
163: public void setPassword(String value) {
164: password = value;
165: }
166:
167: /**
168: * Getter for url property.
169: * @return the database url
170: */
171: public String getUrl() {
172: return url;
173: }
174:
175: /**
176: * Setter for the url property.
177: * @param value the database url
178: */
179: public void setUrl(String value) {
180: url = value;
181: }
182: }
|