大话设计模式实战之工厂方法的实现——链接数据库应用
时间:2021-07-01 10:21:17
帮助过:2人阅读
abstract class AbstractSqlFactory {
//这个就是泛型的类的实例就是需要控制的在JDK1.5中的特性
public abstract <T
extends SqlUtil> T createSql(Class<T>
c);
}
====================================
package com.fxr.工厂方法模式的应用;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public interface SqlUtil {
//数据库的连接
public Connection getConnection();
//关闭数据库的方法的实现
public void closeDB(Connection conn, Statement stmt, ResultSet rs);
}
====================================
package com.fxr.工厂方法模式的应用;
public class SqlFactory
extends AbstractSqlFactory{
@Override
public <T
extends SqlUtil> T createSql(Class<T>
c) {
SqlUtil sqlUtil =
null;
try{
sqlUtil =
(SqlUtil)Class.forName(c.getName()).newInstance();
}catch(Exception e){
System.out.println("出现错误!"
);
}
return (T)sqlUtil;
}
}
==========================================
package com.fxr.工厂方法模式的应用;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlUtilImpl
implements SqlUtil{
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";
// 用户密码
@Override
public Connection 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();
}
return con;
}
@Override
public void closeDB(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();
}
}
}
}
===========================================
package com.fxr.工厂方法模式的应用;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlServerUtilImpl
implements SqlUtil{
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"
;
@Override
public Connection 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();
}
return con;
}
@Override
public void closeDB(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();
}
}
}
}
大话设计模式实战之工厂方法的实现——链接数据库应用
标签: