public abstract classAbstractSqlFactory {//这个就是泛型的类的实例就是需要控制的在JDK1.5中的特性
public abstract T createSql(Classc);
}====================================
packagecom.fxr.工厂方法模式的应用;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.Statement;public interfaceSqlUtil {//数据库的连接
publicConnection getConnection();//关闭数据库的方法的实现
public voidcloseDB(Connection conn, Statement stmt, ResultSet rs);
}====================================
packagecom.fxr.工厂方法模式的应用;public class SqlFactory extendsAbstractSqlFactory{
@Overridepublic T createSql(Classc) {
SqlUtil sqlUtil= null;try{
sqlUtil=(SqlUtil)Class.forName(c.getName()).newInstance();
}catch(Exception e){
System.out.println("出现错误!");
}return(T)sqlUtil;
}
}==========================================
packagecom.fxr.工厂方法模式的应用;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public class MySqlUtilImpl implementsSqlUtil{private static String driver = "com.mysql.jdbc.Driver";//数据库驱动字符串
private static String url = "jdbc:mysql://localhost/studentsystem?useUnicode=true&characterEncoding=utf-8";//连接URL
private static String user = "yonghumingcheng"; //数据库用户名
private static String password = "mima"; //用户密码
@OverridepublicConnection getConnection() {
Connection con=null;try{
Class.forName(driver);
}catch(ClassNotFoundException e){
System.out.println("加载驱动错误信息:" +e.getMessage());
}try{
con=DriverManager.getConnection(url,user,password);
}catch(SQLException e){
System.out.println("数据库连接错误信息:" +e.getMessage());
e.printStackTrace();
}returncon;
}
@Overridepublic voidcloseDB(Connection conn, Statement stmt, ResultSet rs) {//若结果集对象不为空,则关闭
if (rs != null) {try{
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}//若Statement 对象不为空,则关闭
if (stmt != null) {try{
stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
}//若数据库连接对象不为空,则关闭
if (conn != null) {try{
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}===========================================
packagecom.fxr.工厂方法模式的应用;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public class SqlServerUtilImpl implementsSqlUtil{private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";private static final String uri="jdbc:sqlserver://127.0.0.1:1433; DatabaseName=demo.db";private String userName = "yonghumingcheng";private String password = "mima";
@OverridepublicConnection getConnection() {
Connection con=null;try{
Class.forName(driver);
}catch(ClassNotFoundException e){
System.out.println("加载驱动错误信息:" +e.getMessage());
}try{
con=DriverManager.getConnection(driver,userName,password);
}catch(SQLException e){
System.out.println("数据库连接错误信息:" +e.getMessage());
e.printStackTrace();
}returncon;
}
@Overridepublic voidcloseDB(Connection conn, Statement stmt, ResultSet rs) {//若结果集对象不为空,则关闭
if (rs != null) {try{
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
}//若Statement 对象不为空,则关闭
if (stmt != null) {try{
stmt.close();
}catch(Exception e) {
e.printStackTrace();
}
}//若数据库连接对象不为空,则关闭
if (conn != null) {try{
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}