package skydev.modules.data;
import java.sql.*;
/**
* 使用ODBC的方法:<br>
* "sun.jdbc.odbc.JdbcOdbcDriver"<br>
* "jdbc:odbc:" + odbcName<br>
*
* "oracle.thin.Driver"<br>
* "qwe.sql.qweMySqlDriver"<br>
* "symantec.dbanywhere.Driver"<br>
*
* 访问MS SQLServer的方法<br>
* driveName="com.microsoft.jdbc.sqlserver.SQLServerDriver";<br>
* url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";<br>
* 访问MySQL的方法:<br>
* DBDriver=com.mysql.jdbc.Driver<br>
* URL=jdbc:mysql://localhost/demo<br>
*/
public abstract class AbstractConnectionFactory {
private String userName;
private String password;
private String driverName;
private String url;
private java.sql.Connection connection;
/**
* 工厂方法,返回实际创建的连接对象
* @return
*/
/**
* 根据设置的连接参数创建一个新的连接实例
* @return
*/
private Connection getNewConnection() {
try {
this.connection.close(); //试图关闭连接
}
finally {
this.connection = null; //释放连接
try {
Class.forName(this.driverName); //加载驱动程序
try {
this.connection = DriverManager.getConnection(this.url, this.userName,
this.password);
}
catch (SQLException e) {
throw e;
}
}
finally {
return this.connection; //返回新建立的连接
}
}
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public java.sql.Connection getConnection() {
if (connection != null) {
try {






