📄 simpledatasource.java
字号:
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mandarax.examples.db;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* The example uses a <code>SimpleDataSource</code> to connect to the database.
* Works with the settings loaded from the file <strong>exampledb.properties</strong>.
* In particular, the driver class name and the database url are configured in this file.
* The JDBC driver library needs to be in the classpath.
* @since 2.2
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
*/
public class SimpleDataSource implements javax.sql.DataSource , java.io.Serializable {
private String url = null;
private String driver = null;
private String user = null;
private String password = null;
/**
* Constructor.
*/
public SimpleDataSource() {
super();
Properties dbProperties = new Properties();
try {
dbProperties.load(new FileInputStream("exampledb.properties"));
url = dbProperties.getProperty("url");
driver = dbProperties.getProperty("driver");
user = dbProperties.getProperty("user");
password = dbProperties.getProperty("password");
}
catch (IOException x) {
System.err.println("Cannot load file with database settings: exampledb.properties");
}
}
/**
* Get a database connection.
* @return a database connection
*/
public java.sql.Connection getConnection() throws java.sql.SQLException {
return getConnection(user,password);
}
/**
* Get a database connection.
* @return a database connection
* @param userName a user name
* @param password a password
*/
public java.sql.Connection getConnection(String userName, String password) throws java.sql.SQLException {
// try to load driver
try {
Class.forName(driver);
}
catch (Exception x) {
System.err.println("Driver class not found: " + driver);
}
// get connection
return DriverManager.getConnection(url,userName,password);
}
/**
* Get the login timeout.
*/
public int getLoginTimeout() throws SQLException {
return 1000;
}
/**
* Get a log writer.
* @return a log writer
*/
public PrintWriter getLogWriter() throws SQLException {
return null;
}
/**
* Set the login timeout.
* @param millis the time in milli seconds
*/
public void setLoginTimeout(int millis) throws SQLException {}
/**
* Set the log writer.
* @param writer a writer
*/
public void setLogWriter(PrintWriter writer) throws SQLException {}
/**
* Getter for driver property.
* @return the driver class name
*/
public String getDriver() {
return driver;
}
/**
* Setter for the driver property.
* @param value a driver class name
*/
public void setDriver(String value) {
driver = value;
}
/**
* Getter for user property.
* @return the user
*/
public String getUser() {
return user;
}
/**
* Setter for the user property.
* @param value the user
*/
public void setUser(String value) {
user = value;
}
/**
* Getter for password property.
* @return the password
*/
public String getPassword() {
return password;
}
/**
* Setter for the password property.
* @param value the password
*/
public void setPassword(String value) {
password = value;
}
/**
* Getter for url property.
* @return the database url
*/
public String getUrl() {
return url;
}
/**
* Setter for the url property.
* @param value the database url
*/
public void setUrl(String value) {
url = value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -